Search code examples
typescripttype-alias

Why is "type" not a restricted keyword in Typescript?


As per the answers in this question, it is clear to me that type can be a an identifier for a property in classes/interfaces etc.

But why does Typescript permit this? It is not common in my experience that languages allow keywords to be used as identifiers. Why this aberration in TS?

I tried looking through TS docs but couldn't find an answer to this.


Solution

  • TypeScript is meant to be a superset of JavaScript. Every valid JS program is a valid TS program.

    type is not a keyword in JS, which means if it were a "hard" keyword in TS, there would be a compatibility problem: the aforementioned relationship would be broken.

    This is something the creators wanted to avoid, which is why they bent over backwards to ensure that type and a few other keywords are only special in certain contexts but valid identifiers otherwise.