isNotNullOrUndefined
Type guard used for removing nullability from generic type. Great for filtering.
function isNotNullOrUndefined<T>(x: T | null | undefined): x is T {
return x !== null && x !== undefined;
}
Ever-growing list of helpful TypeScript snippets I'm collecting over the years.
Want to share yours? Send me DM on Twitter!
Type guard used for removing nullability from generic type. Great for filtering.
function isNotNullOrUndefined<T>(x: T | null | undefined): x is T {
return x !== null && x !== undefined;
}
Using indexed access to object with keys equal to string literals (will work with number literals as well) allows to easy pattern match union type.
const theme = "dark";
const color = {
dark: "#fff",
light: "#000",
}[theme];