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 variable names, typescript (right now Version 2.1.4 ) will not complain that they are not defined in your program (In the method talk, name is being used without this.). name
is one of those.
If I rename the variable to, say, firstName
, the compiler complains rightfully...
error TS2663: Cannot find name 'firstName'. Did you mean the instance member 'this.firstName'?
Same goes for eg window, which is apparently assumed to exist.
My question(s) are:
The reason it won't complain about name
is that there's a variable called name in the global namespace.
Open the console in the developer tools and write name
and press enter and you'll receive: ""
.
More resources:
All global variables can be used without defining them.
In order to remove all global definitions you can, eg in your tsconfig.json
, set the "libs"
option to an empty array. This will remove all globals.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.