带有duck-typed对象的Typescript字符串文字

Ale*_*oni 10 javascript typescript

Typescript 1.8引入了字符串文字类型.但是,将对象作为参数传递时,如下所示:

const test = {
    a: "hi",
    b: "hi",
    c: "hi"
};

interface ITest {
    a: "hi" | "bye"
}

function testFunc (t: ITest) {

}

testFunc(test);
Run Code Online (Sandbox Code Playgroud)

它失败了:

类型'{a:string; b:字符串; c:字符串; }'不能分配给'ITest'类型的参数.属性'a'的类型是不兼容的.类型'string'不能分配给''hi"|类型 "再见"." 类型'string'不能分配给'"bye"'.

我希望这可以工作,因为它符合界面的要求,但我可能会忽略一些东西.

Dav*_*ret 13

test.a推断的类型是string和否"hi".编译器正在比较类型而不是初始字符串表达式.

为了完成这项工作,您需要将该属性键入"hi" | "bye":

type HiBye = "hi" | "bye";

const test = {
    a: "hi" as HiBye,
    b: "hi",
    c: "hi"
};

interface ITest {
    a: HiBye
}

function testFunc (t: ITest) {
}

testFunc(test);
Run Code Online (Sandbox Code Playgroud)

需要注意的是在原来的情况下,它是没有意义的编译器推断类型的test.a"hi",因为你可以在不同的值赋给test.a它到达之前testFunc(test)-EX.test.a = "not hi".

旁注:编译器不会推断类型是偶数常量字符串变量的字符串表达式.这也会导致很多烦恼......想象一下:

const myVariableTypedAsHi = "hi";   // implicitly typed as "hi"
let otherVar = myVariableTypedAsHi; // otherVar implicitly typed as "hi"

otherVar = "test"; // error: cannot assign `"test"` to `"hi"`—well that would be annoying
Run Code Online (Sandbox Code Playgroud)