Skip to content

Instantly share code, notes, and snippets.

@Manc
Created June 22, 2022 06:41
Show Gist options
  • Save Manc/c3279bda63afde2c2a68fbfb6d052528 to your computer and use it in GitHub Desktop.
Save Manc/c3279bda63afde2c2a68fbfb6d052528 to your computer and use it in GitHub Desktop.
Useful TypeScript code snippets
/**
* Filter array of mixed type elements narrowing down the resulting types,
* e.g. from `string | null` to just `string`.
*/
const mixedArray: Array<string|null> = ['a', 'b', null, ''];
const strings: string[] = mixedArray
.filter(
(value): value is string => typeof value === 'string'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment