考虑在打开strictNullChecks的情况下跟随code:
var a: (number | null)[] = [0, 1, 2, 3, null, 4, 5, 6];
var b: { value: number; }[] = a.map(x => x != null && { value: x }).filter(Boolean);编译失败,原因如下:
Type '(false | { value: number; })[]' is not assignable to type '{ value: number; }[]'.
Type 'false | { value: number; }' is not assignable to type '{ value: number; }'.
Type 'false' is not assignable to type '{ value: number; }'.但是可以肯定的是,.filter(Boolean)会过滤掉false。
null也有同样的问题。
有没有一种方法(除了编写as number[])来标记值不包含false或null
https://stackoverflow.com/questions/47632622
复制相似问题