TypeScript联合类型不适用于const断言(“ as const”)

时间:2019-12-18 17:40:38

标签: javascript typescript union-types

为什么此代码不起作用?

const a = [{ name: 'a', label: 'b' }, { name: 'c', label: 'd' }] as const;
const b = [{ name: 'e', label: 'f' }, { name: 'g', label: 'h' }] as const;
type IArr = typeof a | typeof b;

const fn = (arr: IArr) => arr.filter(el => el.name === 'a');

当我尝试过滤或映射此数组时,Typescript警告: “具有签名,但是这些签名彼此不兼容”

(arr: IArr) => arr.filter(el => {});

但是当我散布这个数组的时候效果很好

(arr: IArr) => [...arr].filter(el => {});

0 个答案:

没有答案