Typescript: The global 'name' variable should be declared as a const of type 'never'

Created on 21 Jul 2016  路  3Comments  路  Source: microsoft/TypeScript

As discussed, the name global variable causes certain problems. We decided that it should be changed to a const with the type never.

Breaking Change Bug lib.d.ts Fixed

Most helpful comment

I think the type of 'name' variable should be 'void' instead of 'never' if we want to prevent misuse.
For example, function f(x: string) { } f(name); this code compiles in the current version but it shouldn't.
The original example from #1351 console.log(name); can't be prevented as long as there is a declaration, though.

All 3 comments

I think the type of 'name' variable should be 'void' instead of 'never' if we want to prevent misuse.
For example, function f(x: string) { } f(name); this code compiles in the current version but it shouldn't.
The original example from #1351 console.log(name); can't be prevented as long as there is a declaration, though.

Accidentally used name (with no previous declaration) in JSX - compiled but failed at runtime.

{name}

If it were declare const name: void instead of declare const name: never, the above would properly fail to compile.

Is there a reason this must be never?

@Hoten #15424 is the long thread to read why never is the best available type for name

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

manekinekko picture manekinekko  路  3Comments

weswigham picture weswigham  路  3Comments

siddjain picture siddjain  路  3Comments