Type signatures for this passed to free-standing functions
-
21-12-2019 - |
Question
Here is the code:
interface Foo
{
c : string
}
function foo()
{
var c = this.c
return c
}
foo.call({ c : "quux" })
Visual Studio says that this : any
so autocompletion of this.c
doesn't work. How do I add a signature to tell that this
implements Foo
so IDE has type info for autocompletion of this.
inside foo
?
Solution
simply create a temp variable:
var self:Foo = this;
var c = self.c
return c
There is an open issue to specify what this
means in an arbitrary context that you can vote/contribute to : https://typescript.codeplex.com/workitem/507
OTHER TIPS
Since the function foo
could be used with any this
context, you'd need to use a type assertion:
var c = (<Foo>this).c;
Otherwise, the signature can't be inferred.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow