Skip to content

Instantly share code, notes, and snippets.

@wesbos
Last active November 4, 2022 14:00
Show Gist options
  • Save wesbos/20babb2f78acf596f4d24322d0e4fbe0 to your computer and use it in GitHub Desktop.
Save wesbos/20babb2f78acf596f4d24322d0e4fbe0 to your computer and use it in GitHub Desktop.
// 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