0

I am not sure if I am misunderstanding how Exclude works but I have the following issue:

export interface Base {
    page: number;
    count: number;
}

export interface Sub extends Partial<Exclude<Base, 'count'>> { // error
    count?: 'nonzero'|'zero';
}

The interface Sub is causing an error saying Sub incorrectly extends interface Partial<Base> however this is not what I was expecting. I was expecting that Sub extends Partial<{ page: number }> which is what I thought was the type of Exclude<Base, 'count'> however it does not seem to be the case.

Playground

apokryfos
  • 38,771
  • 9
  • 70
  • 114

1 Answers1

1

Exclude removed a type from a union. So for example Exclude<'a' | 'b', 'b'> will be a. It does not remove properties from a type.

Generally the type that removed properties from another type is called Omit. Omit will be included in 3.5, but is simple to define in terms of Pick and Exclude (below is actually the 3.5 definition of Omit):

type Omit<T, K extends PropertyKey> = Pick<T, Exclude<keyof T, K>>
export interface Base {
    page: number;
    count: number;
}

export interface Sub extends Partial<Omit<Base, 'count'>> { // ok
    count?: 'nonzero'|'zero';
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357