Consider following code with strictNullChecks
turned on:
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);
It fails to compile due to:
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; }'.
But it is absolutely clear, that false
will be filtered away by .filter(Boolean)
.
Same problem with null
.
Is there a way (except writing as number[]
) to mark that value doesn't contain false
or null
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…