TypeScript: Property does not exist on type '{}'

后端 未结 7 1811
忘掉有多难
忘掉有多难 2021-01-31 13:25

I am using Visual Studio 2013 fully patched. I am trying to use JQuery, JQueryUI and JSRender. I am also trying to use TypeScript. In the ts file I\'m getting an error as follo

7条回答
  •  暖寄归人
    2021-01-31 13:53

    Access the field with array notation to avoid strict type checking on single field:

    data['propertyName']; //will work even if data has not declared propertyName
    

    Alternative way is (un)cast the variable for single access:

    (data).propertyName;//access propertyName like if data has no type
    

    The first is shorter, the second is more explicit about type (un)casting


    You can also totally disable type checking on all variable fields:

    let untypedVariable:any= {}; //disable type checking while declaring the variable
    untypedVariable.propertyName = anyValue; //any field in untypedVariable is assignable and readable without type checking
    

    Note: This would be more dangerous than avoid type checking just for a single field access, since all consecutive accesses on all fields are untyped

提交回复
热议问题