patterntypescriptCritical
What is the "type" reserved word in TypeScript?
Viewed 0 times
typescriptreservedwhatthewordtype
Problem
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:
Let's say that you then try to implement the interface in a class, like this:
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?
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?
Solution
It's used for "type aliases". For example:
For reference, there is a section for type aliases in the TypeScript Handbook.
type StringOrNumber = string | number;
type DictionaryOfStringAndPerson = Dictionary;For reference, there is a section for type aliases in the TypeScript Handbook.
Code Snippets
type StringOrNumber = string | number;
type DictionaryOfStringAndPerson = Dictionary<string, Person>;Context
Stack Overflow Q#31364693, score: 183
Revisions (0)
No revisions yet.