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
The compiler assumes that the scope of this is the instance of the TestClass. However, if the method is called via .apply(), as in the example above, it is possible to hijack the this scope of the method.
This is a bit of an edge case, since most TypeScript code shouldn't be circumventing the type safety by using the .apply() method. However, some third-party JavaScript libraries rely on the .apply() method, which undermines confidence in the this keyword.
The fix is to redefine the method as a property using the fat arrow syntax, i.e.:
Given the following:
The compiler assumes that the scope of
this
is the instance of the TestClass. However, if the method is called via.apply()
, as in the example above, it is possible to hijack thethis
scope of the method.This is a bit of an edge case, since most TypeScript code shouldn't be circumventing the type safety by using the
.apply()
method. However, some third-party JavaScript libraries rely on the.apply()
method, which undermines confidence in thethis
keyword.The fix is to redefine the method as a property using the fat arrow syntax, i.e.:
Perhaps the compiler should only enforce type safety on
this
if the method is defined such that thethis
keyword is lexically scoped?The text was updated successfully, but these errors were encountered: