请考虑以下类型脚本示例:
接口myinterface{
prop1:字符串;
Prop2:数字;
Prop3:字符串;
}
const myvar=“prop4”作为myInterface的键;
在Visual Studio 2017中运行此代码,Visual Studio代码和操场中的代码成功编译(typescript 2.9.2);字符串值不是针对myinterface进行类型检查的
but both vs and vsc show the 3 properties ofmyinterfaceas intellisense suggestions:。

const myvar:keyof myinterface=“prop4”;expectly works as intended and throws an error but the first example neither throws an error,nor secures type safety.
这份声明合法吗?如果是这样,它应该如何表现?如果不是,为什么要编译?
在Visual Studio 2017中运行此代码时,Visual Studio代码和操场中的代码成功编译(typescript 2.9.2);字符串值不是根据MyInterface
但是vs和vsc都显示了我的接口
作为IntelliSense建议:

const myVar: keyof MyInterface = "Prop4";
显然按预期工作并抛出错误,但第一个示例既不抛出错误,也不确保类型安全。
这份声明合法吗?如果是这样,它应该如何表现?如果不是,为什么要编译?