TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Is there a way to use TypeScript to prevent accidental global access?

Open bcherny opened this issue 7 years ago • 53 comments

I just spent 2 hours tracking down this issue:

  • We have a class with a prototype method called focus()
  • Our code was calling focus(), but it should have been calling this.focus()
  • The code compiled fine, because window.focus() shares the same signature as our focus() method

Is there a way to throw a compile time error when implicitly accessing global methods (on window, global, etc.)?

If not, a compiler flag would be extremely helpful. I would happily be more explicit about commonly used globals (window.setTimeout, window.document, ...) if it meant I could more easily catch insidious bugs like this one.

Full commit here: https://github.com/coatue/SlickGrid/commit/0f8bab3fa9968173d749ccdf535c88f3a526ca8b.

bcherny avatar Feb 25 '17 01:02 bcherny