Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 什么是「;“类型”;打字稿中的保留字?_Typescript_Keyword_Reserved Words - Fatal编程技术网

Typescript 什么是「;“类型”;打字稿中的保留字?

Typescript 什么是「;“类型”;打字稿中的保留字?,typescript,keyword,reserved-words,Typescript,Keyword,Reserved Words,我刚刚注意到,当试图在TypeScript中创建一个接口时,“type”要么是关键字,要么是保留字。例如,在创建以下界面时,在Visual Studio 2013中,TypeScript 1.4以蓝色显示“type”: interface IExampleInterface { type: string; } 假设您尝试在类中实现接口,如下所示: class ExampleClass implements IExampleInterface { public type: str

我刚刚注意到,当试图在TypeScript中创建一个接口时,“type”要么是关键字,要么是保留字。例如,在创建以下界面时,在Visual Studio 2013中,TypeScript 1.4以蓝色显示“type”:

interface IExampleInterface {
    type: string;
}
假设您尝试在类中实现接口,如下所示:

class ExampleClass implements IExampleInterface {
    public type: string;

    constructor() {
        this.type = "Example";
    }
}
在类的第一行中,当您键入(对不起)单词“type”以实现接口所需的属性时,IntelliSense将显示“type”,其图标与其他关键字(如“typeof”或“new”)相同

我环顾四周,可以找到这个在TypeScript中将“type”列为“strict mode reserved word”的列表,但我没有找到关于其实际用途的更多信息

我怀疑我是在放屁,这是我应该知道的,但是TypeScript中的“type”保留字是用来做什么的?

它是用来做“type别名”的。例如:

type StringOrNumber = string | number;
type DictionaryOfStringAndPerson = Dictionary<string, Person>;
类型StringOrNumber=string | number;
类型DictionaryOfStringAndPerson=字典;
参考:(编辑:删除过时链接)TypeScript规范v1.5(第3.9节,“类型别名”,第46和47页)

更新:(编辑:删除过时链接)现在进入1.8规范第3.10节。感谢@RandallFlagg更新规范和链接

更新:(编辑:不推荐的链接),搜索“类型别名”可以找到相应的部分

更新:现在是。

在typescript中键入关键字: 在typescript中,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;
您可以组合标量类型(
string
number
等),也可以组合文本值,如
1
'mystring'
。您甚至可以组合其他用户定义类型的类型。例如,类型
madness
中包含类型
random
color

然后,当我们尝试将字符串文字化时(我们的IDE中有IntelliSense),它会显示一些建议:


它显示了所有的颜色,类型madness源于类型color,'random'源于类型random,最后是字符串
'foo'
,它位于类型madness本身上。

是的,所以这是显而易见的。事实证明,当你在编程语言的上下文中搜索“type”这个词时,很难找到你想要的东西——特别是当所讨论的语言被称为“TypeScript”时。顺便说一下,TypeScript中还没有通用字典,对吗?如果您需要它(对于TS 0.9及以上版本):谢谢,我想我以前看过一次该项目,当时我想要一本字典,但最后我决定不使用。该概念来自c中的typedef。检查此项,前两个链接已断开。请更新,谢谢:)
类型别名
为您的类型提供语义名称:用户定义的类型和enumerables@ORcoder好问题!我也想知道。一个很好的解释让我们明白!其中您已声明
type color=“blue”|“red”|“yellow”|“purple”类内或类外的语句?关于用户定义类型和枚举之间的差异