What is the "type" reserved word in TypeScript?

Adam Goodwin picture Adam Goodwin · Jul 12, 2015 · Viewed 40.4k times · Source

I just noticed when trying to create an interface in TypeScript that "type" is either a keyword or a reserved word. When creating the following interface, for example, "type" is shown in blue in Visual Studio 2013 with TypeScript 1.4:

interface IExampleInterface {
    type: string;
}

Let's say that you then try to implement the interface in a class, like this:

class ExampleClass implements IExampleInterface {
    public type: string;

    constructor() {
        this.type = "Example";
    }
}

In the first line of the class, as you type (sorry) the word "type" in order to implement the property required by the interface, IntelliSense appears with "type" having the same icon as other keywords like "typeof" or "new".

I've had a look around, and could find this GitHub issue which lists "type" as a "strict mode reserved word" in TypeScript, but I have not found any further information about what its purpose actually is.

I suspect I'm having a brain fart and this is something obvious I should already know, but what is the "type" reserved word in TypeScript for?

Answer

Jcl picture Jcl · Jul 12, 2015

It's used for "type aliases". For example:

type StringOrNumber = string | number;
type DictionaryOfStringAndPerson = Dictionary<string, Person>;

Reference: (edit: removed outdated link) TypeScript Specification v1.5 (section 3.9, "Type Aliases", pages 46 & 47)

Update: (edit: removed outdated link) Now on section 3.10 of the 1.8 spec. Thanks @RandallFlagg for the updated spec and link

Update: TypeScript Handbook, search "Type Aliases" can get you to the corresponding section.