You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
No referencing/intellisense fo nested properties/functions within class #7801
I have the following structure where I have a class that contains functions nested inside properties and vice versa. Intellisense and therefore referencing is not working when inside a function that resides inside a property of the class. Is there a way to solve this sort of issue so that I can reference myThirdProp inside mySecondFunc?
export class a {
myProperty = 'hello';
myFunc() {
this.myProperty; //referencing and intellisense available
}
mySecondProp: Object = {
myThirdProp: 'hello2',
mySecondFunc() {
this. //no intellisense/reference here
}
}
}
Currently, we don't really know how mySecondProp.mySecondFunc is going to be invoked, so its this type is any. #6739 should solve this for you by allowing you to specify the type of this inside mySecondFunc.
this inside of an object literal now has Intellisense. @zalow517 can you try typescript@next? It will be available April 8, after the nightly build picks it up.
We have tried different ways of addressing this issue, and the current behavior (this in an object literal is of type any) seems to be the most reasonable.
Classes present stronger assumptions about how the functions will be called. object literals on the other hand can be used as a property bag often with no assumptions about the relationship of the methods and properties on them. we have seen common JS patterns where methods are added, removed, or called with a different this target at runtime (see #8133 for an example).
the recommendation is to specify an explicit this type in methods:
Activity
RyanCavanaugh commentedon Apr 4, 2016
Currently, we don't really know how
mySecondProp.mySecondFuncis going to be invoked, so itsthistype isany. #6739 should solve this for you by allowing you to specify the type ofthisinsidemySecondFunc.sandersn commentedon Apr 7, 2016
thisinside of an object literal now has Intellisense. @zalow517 can you trytypescript@next? It will be available April 8, after the nightly build picks it up.sandersn commentedon Apr 30, 2016
#8389 reverts this fix for now.
mhegazy commentedon Apr 30, 2016
Cross positing from #8110 (comment)
We have tried different ways of addressing this issue, and the current behavior (this in an object literal is of type any) seems to be the most reasonable.
Classes present stronger assumptions about how the functions will be called. object literals on the other hand can be used as a property bag often with no assumptions about the relationship of the methods and properties on them. we have seen common JS patterns where methods are added, removed, or called with a different this target at runtime (see #8133 for an example).
the recommendation is to specify an explicit
thistype in methods:ahejlsberg commentedon Feb 17, 2017
See #14141.