Declaring the type of 'this' in a typescript function?

前端 未结 4 1170
盖世英雄少女心
盖世英雄少女心 2020-12-01 17:32

I\'m writing a grunt task in TypeScript. I\'m trying to translate something I already have in JavaScript.

So, when grunt runs a task, it runs a function. When it ru

4条回答
  •  天命终不由人
    2020-12-01 18:31

    While I found that is now possible with this:

    class ClassyClass {
        prop = 'Juicy Strings'
    }
    
    function x( this: ClassyClass ) {
        console.log( this.prop )
    }
    

    I have come prefer an alternative that doesn't take up real estate in the arguments line

    function x() {
        const that: ClassyClass = this
    
        console.log( that.prop )
    }
    

提交回复
热议问题