Search code examples
javascriptreactjstypescriptwebfonts

How to declaring a function with properties in typescript react


@Bind()
sdffsf() {
 this.sdffsf.fff = 'sdfsd';
}

like this, typescript always reminds me fff didn't exist wanna know how to declare the interface of this method


Solution

  • I learned something new here.

    I found this answer https://stackoverflow.com/a/28920916/13257176:

    they also linked to TS this doc: https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function

    sdffsf(this: {sdffsf:{fff:string}}) {
       this.sdffsf.fff = 'sdfsd';
    }
    

    I did not test this, you might have to make this a function or class method. Ie:

    function sdffsf(this: {sdffsf:{fff:string}}) {
       this.sdffsf.fff = 'sdfsd';
    }
    

    or

    class x {
      sdffsf(this: {sdffsf:{fff:string}}) {
        this.sdffsf.fff = 'sdfsd';
      }
    }