Skip to content

Instantly share code, notes, and snippets.

@martinratinaud
Last active February 27, 2020 06:54
Show Gist options
  • Save martinratinaud/ec2799c43f6e6a0f23637910ad1871d0 to your computer and use it in GitHub Desktop.
Save martinratinaud/ec2799c43f6e6a0f23637910ad1871d0 to your computer and use it in GitHub Desktop.
Typescript Conditional Types - The simple example
// full article https://medium.com/@martinratinaud/typescript-conditional-types-the-simple-example-8c971c04b6bc
// playground https://www.typescriptlang.org/play/#code/PTAEDEFcBttBDATgFwJYGNoFNQHsB2oAFssgA4DOAXCALZYAmqktAdOrrcAAK1Jr5E8AfEgNgyAJ5ksFdIlRlkAWg74maAvGjKpMirqJZlFVLTLZlWAB7xzlgBzoAnAHYAjOgAMAFgBGAGx+6ABQIXo4AJIAKtI4ALygAOTQkgroFEmgAD7JFBaoyEkA3OFxoJEUADJpGBQA8ogAygXIADzRAHygidGgNshY6hTJqemZoAD8oAAKiJyoFFhtAERjdSvdVKAr+dCFK6UhahTIoABmkPjoPaAAFBHbMXEAlE-VtRmNLfvtzzLdeLdADeIVA4NAqHO9wiPXiiRSn0yL1AoIh6NAiCwyEgiEIcwWS1YWIouGgADcsHdEeMki9SuiAL5giFYnF4vKtEohZlAA
type IType = 'lyrics' | 'split';
type IsLyricsOrSplit<T> = T extends 'lyrics' ? Promise<"lyrics"> : "split";
const func = (type: IType): IsLyricsOrSplit<IType> => {
if (type === 'lyrics') {
return Promise.resolve('lyrics');
}
return 'split';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment