Although the built-in type of subtraction does not exist, it can currently be hacked as follows:
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 Omit<
O,
D extends string,
// issue 16018
Foo extends Sub0<keyof O, D> = Sub0<keyof O, D>
> = Pick<O, Foo[keyof O]>
In this case, you would do:
type ExcludeCart<T> = Omit<T, 'cart'>
With TypeScript> = 2.6 you can simplify it to:
export type Sub<
O extends string,
D extends string
> = {[K in O]: (Record<D, never> & Record<string, K>)[K]}[O]
export type Omit<O, D extends string> = Pick<O, Sub<keyof O, D>>
check it out on the playground
source
share