Typescript 1.8引入了字符串字面量类型。然而,在将对象作为参数传递时,例如以下情况:
const test = {
a: "hi",
b: "hi",
c: "hi"
};
interface ITest {
a: "hi" | "bye"
}
function testFunc (t: ITest) {
}
testFunc(test);
出现以下错误:
类型为'{ a: string; b: string; c: string; }'的参数无法分配给类型为'ITest'的参数。 属性'a'的类型不兼容。 类型'string'无法分配给类型'"hi" | "bye"'。 类型'string'无法分配给类型'"bye"'。
我原本以为这个代码符合接口要求,但可能是我的疏忽。