Adam Goodwin
Adam Goodwin

Reputation: 4071

What is the "type" reserved word in TypeScript?

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?

Upvotes: 150

Views: 87546

Answers (3)

Jcl
Jcl

Reputation: 28272

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

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

For reference, there is a section for type aliases in the TypeScript Handbook.

Upvotes: 184

jumping_monkey
jumping_monkey

Reputation: 7865

Type Aliases allow defining types with a custom name (an Alias).

Type Aliases can be used for primitives like string or more complex types such as objects and arrays:

Example:

type CarYear = number
type CarType = string
type CarModel = string
type Car = {
  year: CarYear,
  type: CarType,
  model: CarModel
}

const carYear: CarYear = 2001
const carType: CarType = "Toyota"
const carModel: CarModel = "Corolla"
const car: Car = {
  year: carYear,
  type: carType,
  model: carModel
};

Reference

Upvotes: 1

Willem van der Veen
Willem van der Veen

Reputation: 36660

Type keyword in typescript:

In typescript the type keyword defines an alias to a type. We can also use the type keyword to define user defined types. This is best explained via an example:

type Age = number | string;    // pipe means number OR string
type color = "blue" | "red" | "yellow" | "purple";
type random = 1 | 2 | 'random' | boolean;

// random and color refer to user defined types, so type madness can contain anything which
// within these types + the number value 3 and string value 'foo'
type madness = random | 3 | 'foo' | color;  

type error = Error | null;
type callBack = (err: error, res: color) => random;

You can compose types of scalar types (string, number, etc), but also of literal values like 1 or 'mystring'. You can even compose types of other user-defined types. For example type madness has the types random and color in it.

Then when we try to make a string literal our (and we have IntelliSense in our IDE) it shows suggestions:

enter image description here

It shows all the colors, which type madness derives from having type color, 'random' which is derived from type random, and finally, the string 'foo' which is on the type madness itself.

Upvotes: 51

Related Questions