Last active
November 4, 2022 14:00
-
-
Save wesbos/20babb2f78acf596f4d24322d0e4fbe0 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
// Here we check if our custom type "Stringy" extends a narrow type of '' | |
// If it does, the type is never | |
// If it doesnt, the type is "Strinfy", which is just a string type | |
function getItem<Stringy extends string>( | |
id: Stringy extends '' ? never : Stringy | |
) { | |
// code here | |
} | |
// works: | |
getItem('abc123'); // No error | |
// Error | |
getItem(''); | |
getItem(); | |
getItem(123); // fail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment