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 - Fatal编程技术网

Typescript:仅添加可选属性将删除类型检查

Typescript:仅添加可选属性将删除类型检查,typescript,Typescript,我使用的是typescript 1.4.1,代码如下: var hello: {message: string}; hello = {world: 't'}; 按照预期,typescript编译器会输出一个错误: 类型“{world:string;}”不能分配给类型“{message: 字符串;}'。类型{world: 字符串;}'。(var)hello:{message:string;} 但如果类型仅包含可选属性,如: var hello: {message?: string}; hello

我使用的是typescript 1.4.1,代码如下:

var hello: {message: string};
hello = {world: 't'};
按照预期,typescript编译器会输出一个错误:

类型“{world:string;}”不能分配给类型“{message: 字符串;}'。类型{world: 字符串;}'。(var)hello:{message:string;}

但如果类型仅包含可选属性,如:

var hello: {message?: string};
hello = {world: 't'};
然后编译器会非常高兴,但我希望能够检测到错误

我做错了什么?还是一个打字脚本错误

谢谢你的帮助

然后编译器会非常高兴,但我希望能够检测到错误。 我做错了什么?还是一个打字脚本错误


不,这是故意的。只要所需的信息(在本例中没有)存在,额外的信息就可以了

好的,谢谢!但是我认为当你有一个充满可选属性的界面(比如HighChartsOptions)并且你打字时,这是非常误导人的。在这种情况下,Typescript编译器对您没有任何帮助。下面是关于“为什么”的更多信息: