Skip to content

Instantly share code, notes, and snippets.

@NaridaL
Last active October 17, 2017 22:00
Show Gist options
  • Save NaridaL/0f79614cf5b1a8aa0d48d2dc43f60209 to your computer and use it in GitHub Desktop.
Save NaridaL/0f79614cf5b1a8aa0d48d2dc43f60209 to your computer and use it in GitHub Desktop.
// see also https://www.npmjs.com/package/subtractiontype.ts
// for literal unions
type Sub0<
O extends string,
D extends string,
> = {[K in O]: (Record<D, never> & Record<string, K>)[K]}
type Sub<
O extends string,
D extends string,
// issue 16018
Foo extends Sub0<O, D> = Sub0<O, D>
> = Foo[O]
type Dissoc<
O,
D extends string,
// issue 16018
Foo extends Sub0<keyof O, D> = Sub0<keyof O, D>
> = Pick<O, Foo[keyof O]>
// example
type Input = { X: 1, Y: 2, Z: 3 }
type Expected = { Y: 2, Z: 3 }
type Output = Dissoc<'X', Input>
type Check<
To extends Output = Expected,
From extends Expected = Output
> = never
// for literal unions
// example
type Output2 = Sub<'X', 'Y' | 'X'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment