Created
July 7, 2020 07:04
-
-
Save jLouzado/ebecd3bc602ed87e289be7b6a5b0fde7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface superType { | |
a: string, | |
b: number, | |
} | |
interface subType extends superType { | |
c: string | number | |
} | |
declare const xs: Array<superType | null> | Array<subType> | |
const isNotNull = <T>(a: T): a is NonNullable<T> => a !== null | |
/** Should be enough to guarantee no-nulls but somehow isn't */ | |
const whyAreThereNulls = xs.filter(isNotNull) // => (superType | null)[] | |
// need to add another filter before TS gives all-clear | |
const definitelyNoNulls = xs.filter(isNotNull).filter(isNotNull) // superType[] | |
/*** simpler type works */ | |
declare const ys: Array<superType | null> | |
const oneFilter = ys.filter(isNotNull) // superType[] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment