Why typescript doesn't complain about some undefined variables

I have the following example:

class Uncle { constructor(public name : string) { } talk() { return "Hello my name is " + name; } } let p : Uncle = new Uncle("Jo"); console.log(p.talk()); 

For certain typescript variable names (right now Version 2.1.4 ) will not complain that they are not defined in your program (in a method call, the name is used without it.). name is one of those.

If I rename a variable, say firstName , the compiler complains in truth ...

error TS2663: Cannot find the name 'firstName'. Did you mean an instance of "this.firstName"?

The same thing happens, for example. a window that is apparently supposed to exist.

My question (s):

  • What variable names are supposed to exist and why?
  • Is it possible to change this behavior (for example, in some lints you can specify which variables you expect to receive globally)?
+6
source share
1 answer

The reason he won't complain about name is because there is a variable called the name in the global namespace.
Open the console in the developer tools and write name and press enter, and you will get: "" .

Additional resources:

All global variables can be used without defining them.

To remove all global definitions, you can, for example. in tsconfig.json , set the "libs" parameter to an empty array. This will remove all global variables.

+3
source

Source: https://habr.com/ru/post/1013289/


All Articles