这两个示例的行为应该是相同的,但第二个示例是错误的。为什么?
// Example 1:
const a: 'x' | 'y' = 'x';
const b: ['x'] | ['y'] = [a]; // ok
// Example 2:
function fn(a: 'x' | 'y') {
const b: ['x'] | ['y'] = [a];
// ^
// Type '["x" | "y"]' is not assignable to type '["x"] | ["y"]'.
// Type '["x" | "y"]' is not assignable to type '["x"]'.
// Type '"x" | "y"' is not assignable to type '"x"'.
// Type '"y"' is not assignable to type '"x"'.
}
你可以使用try it on the playground。
https://stackoverflow.com/questions/56174696
复制相似问题