Skip to content

Instantly share code, notes, and snippets.

@jLouzado
Created July 7, 2020 07:04
Show Gist options
  • Save jLouzado/ebecd3bc602ed87e289be7b6a5b0fde7 to your computer and use it in GitHub Desktop.
Save jLouzado/ebecd3bc602ed87e289be7b6a5b0fde7 to your computer and use it in GitHub Desktop.
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