首页 > 解决方案 > VsCode 在类型转换时没有给出语法错误

问题描述

interface TypeA {
    prop1: string;
    prop2: string;
}
const b = <TypeA> { prop1: 'str', prop3: 'str' };
console.log(b.prop1);

vscode 在这种情况下没有给出语法错误。我想要“prop2 is missing”错误

标签: javascripttypescriptvisual-studio-code

解决方案


将类型放在变量名上。然后你就有了你想要的。

interface TypeA {
    prop1: string;
    prop2: string;
}
const b: TypeA = { prop1: 'str', prop3: 'str' };
console.log(b.prop1);

推荐阅读