Created
June 22, 2022 06:41
-
-
Save Manc/c3279bda63afde2c2a68fbfb6d052528 to your computer and use it in GitHub Desktop.
Useful TypeScript code snippets
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
/** | |
* 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