TypeScript 中的“类型”保留字是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31364693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the "type" reserved word in TypeScript?
提问by Adam Goodwin
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:
我只是在尝试在 TypeScript 中创建接口时注意到“类型”是关键字或保留字。例如,在创建以下界面时,“类型”在带有 TypeScript 1.4 的 Visual Studio 2013 中显示为蓝色:
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".
在类的第一行中,当您键入(抱歉)单词“type”以实现界面所需的属性时,IntelliSense 会显示“type”,该图标与“typeof”或“new”等其他关键字具有相同的图标”。
I've had a look around, and could find this GitHub issuewhich lists "type" as a "strict mode reserved word" in TypeScript, but I have not found any further information about what its purpose actually is.
我环顾四周,可以找到这个GitHub 问题,它在 TypeScript 中将“类型”列为“严格模式保留字”,但我没有找到有关其实际用途的任何进一步信息。
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?
我怀疑我有一个脑放屁,这很明显我应该已经知道了,但是 TypeScript 中的“类型”保留字是什么?
回答by Jcl
It's used for "type aliases". For example:
它用于“类型别名”。例如:
type StringOrNumber = string | number;
type DictionaryOfStringAndPerson = Dictionary<string, Person>;
Reference: TypeScript Specification v1.5(section 3.9, "Type Aliases", pages 46 & 47)
参考:TypeScript 规范 v1.5(第 3.9 节,“类型别名”,第 46 和 47 页)
Update: Now on section 3.10 of the 1.8 spec. Thanks @RandallFlagg for the updated spec and link
更新:现在是 1.8 规范的第 3.10 节。感谢@RandallFlagg 提供更新的规范和链接
Update: TypeScript Handbook, search "Type Aliases" can get you to the corresponding section.
更新:TypeScript Handbook,搜索“Type Aliases”可以跳转到对应的章节。
回答by Willem van der Veen
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 关键字定义了类型的别名。我们还可以使用 type 关键字来定义用户定义的类型。这最好通过一个例子来解释:
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.
您可以组合标量类型(string
、number
等),也可以组合字面值(如1
或 )'mystring'
。您甚至可以组合其他用户定义类型的类型。例如 typemadness
有类型random
和color
在里面。
Then when we try to make a string literal our (and we have IntelliSense in our IDE) it shows suggestions:
然后,当我们尝试创建一个字符串字面量(并且我们的 IDE 中有 IntelliSense)时,它会显示建议:
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.
它显示了所有颜色,其中类型 madness 派生自类型 color,'random' 派生自 random 类型,最后,'foo'
类型 madness 本身上的字符串。