James Hopkin
James Hopkin

Reputation: 13973

Why does Typescript think the symbol 'name' always exists?

I've noticed mistakes in my Typescript being accepted by the compiler when I've written name when I meant to write someObject.name.

As a test, I tried compiling a single file containing only console.log(name) and tsc accepts it. Does Typescript have some global symbols defined?

I'm using Typescript 2.8.1 and have no global packages installed except NPM itself and Typescript.

Upvotes: 1

Views: 65

Answers (1)

JeffRSon
JeffRSon

Reputation: 11206

Yes, there's a (default) lib for EcmaScript standard, say ES2016. This lib is called "lib.es2016.full.d.ts" (https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es2016.full.d.ts) and it contains the "name" declaration.

In your editor (if it's not just notepad, but Visual Studio Code for example) you could select "Go to definition" from context menu. Or you could hover above "name" and see, which type "name" actually is (it's "never" and const).

This had been done to restrict access to global (Window) name property: https://github.com/Microsoft/TypeScript/issues/1351 https://github.com/Microsoft/TypeScript/issues/9850

Upvotes: 3

Related Questions