Skip to content

Instantly share code, notes, and snippets.

@Tirke
Created November 29, 2019 16:31
Show Gist options
  • Save Tirke/d95d3da7b79f1dc7d1f3adbf25710de3 to your computer and use it in GitHub Desktop.
Save Tirke/d95d3da7b79f1dc7d1f3adbf25710de3 to your computer and use it in GitHub Desktop.
type Config<T> =
T extends { type: 'sftp' } ? { login: string, password: string } :
T extends { type: 'post' } ? { url: string, header: string } : {};
const union: { type: 'post' | 'sftp' } = { type: 'post' }
const simple: { type: 'post' } = { type: 'post' }
const splitUnion: { type: 'post' } | { type: 'sftp' } = { type: 'post' }
type Instance = Config<typeof union> // ==> Instance is {} and not {url: string, header: string}
type InstanceOk = Config<typeof simple> // ==> Instance is { url: string, header: string }
type InstanceOk2 = Config<typeof splitUnion> // ==> Instance is { url: string, header: string }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment