145

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?

Adam Goodwin
  • 3,951
  • 5
  • 28
  • 33
  • 1
    `Type Alias` to give your types a semantic name : http://basarat.gitbooks.io/typescript/content/docs/types/type-system.html – basarat Jul 12 '15 at 09:01

3 Answers3

182

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: (edit: deprecated link) TypeScript Handbook, search "Type Aliases" can get you to the corresponding section.

Update: Now it's here in the TypeScript Handbook.

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • 40
    Yep, so it was something obvious. Turns out it's quite hard to find what you're looking for when you search for the word "type" in the context of programming languages – particularly when the language in question is called "TypeScript". By the way, generic dictionaries still don't exist in TypeScript right? – Adam Goodwin Jul 12 '15 at 05:48
  • 1
    [Here's one](https://github.com/basarat/typescript-collections) if you need it (for TS 0.9 and above): – Jcl Jul 12 '15 at 05:52
  • Thanks, I think I saw that project once before when I wanted a dictionary, but in the end I decided just to do without. – Adam Goodwin Jul 12 '15 at 06:07
  • the concept came from typedef in c a check this https://www.studytonight.com/c/typedef.php – Pranoy Sarkar Jul 15 '18 at 07:55
  • The first two links are broken. Please update, thanks :) – Serkan Sipahi Sep 16 '20 at 18:39
  • @SerkanSipahi removed outdated links, although left the text for clarity. Thanks :-) – Jcl Sep 17 '20 at 08:10
50

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.

Sinandro
  • 2,426
  • 3
  • 21
  • 36
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
0

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

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58