interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
let user = { firstName: "Jane", lastName: "User" };
document.body.innerHTML = greeter(user);
这段代码ts代码编译没问题
但是TSLint却报了一个很诡异的错
类型“{ firstName: string; lastName: string; }”的参数不能赋给类型“Person”的参数。
Type '{ firstName: string; lastName: string; }' is missing the following properties from type 'Person': name, age, getName
明明接口没有定义name, age, getName为啥说我缺失?
重启 vscode 试试,有时候 vscode 的 ts 检测会有延迟和错乱。