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

后端 未结 7 1809
忘掉有多难
忘掉有多难 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 14:03

    When you write the following line of code in TypeScript:

    var SUCSS = {};
    

    The type of SUCSS is inferred from the assignment (i.e. it is an empty object type).

    You then go on to add a property to this type a few lines later:

    SUCSS.fadeDiv = //...
    

    And the compiler warns you that there is no property named fadeDiv on the SUCSS object (this kind of warning often helps you to catch a typo).

    You can either... fix it by specifying the type of SUCSS (although this will prevent you from assigning {}, which doesn't satisfy the type you want):

    var SUCSS : {fadeDiv: () => void;};
    

    Or by assigning the full value in the first place and let TypeScript infer the types:

    var SUCSS = {
        fadeDiv: function () {
            // Simplified version
            alert('Called my func');
        }
    };
    

提交回复
热议问题