Improve type safety of `name` global variable
falsandtru opened this issue · 5 comments
name global variable is still assignable to string type. So name is still unsafe. name: string | undefined works better than current definition name: never.
TypeScript Version: master
Code
const n1: string = <never>name; // pass
const n2: string = <string | never>name; // pass
const n3: string = <string | undefined>name; // error, expectedExpected behavior:
declare const name: string | undefined;Actual behavior:
declare const name: never;related: #9850 @DanielRosenwasser
@mhegazy @DanielRosenwasser Can you please also review this?
@DanielRosenwasser any thoughts
The resons we made it never was that ppl were stepping on it by mistake..
class C {
name: string;
method() {
doSomething(name); // not this.name
}
}
```
by making it `never` at least you will get an error and you will go back and check the source of the declaration.
`string | undefined` will put us back where we started for most users.as noted in #15424 (comment), never was added for a specific reason. It is much more common for users to use the global name by mistake vs intentionally. this change was meant to alert users in these cases; unfortunately it breaks other cases, but we believe this gets the 90% case right. for other cases, window.name.