Jacob Stamm
Jacob Stamm

Reputation: 1828

How to "weaken" multiple properties of a type

When working with autogenerated class/interface files (through something like Swagger Codegen), it often becomes necessary to modify these models. For instance, suppose you have an autogenerated HighSchool class.

export interface HighSchool {
    schoolTypeCode: number;
    name: string;
    state: string;
    city: string;
}

Let's say we create an enum to make working with known schoolTypeCode values easy.

export const enum HighSchoolTypeCode {
    USHighSchool,
    GED,
    OnlineSchool
}

Rather than modifying the autogenerated HighSchool interface to use this enum, which will be overwritten once autogeneration is reexecuted, I can create my own HighSchool interface in a separate file and retype the schoolTypeCode by "weakening" it using a helper type, as described in this GitHub issue response.

export interface HighSchool extends Weaken<HighSchool, 'schoolTypeCode'> {
  schoolTypeCode?: HighSchoolTypeCode;
}

Here is the Weaken helper type:

type Weaken<T, K extends keyof T> = {
  [P in keyof T]?: P extends K ? any : T[P];
};

The limitation here is that only one property can be weakened at a time. Can this helper type be reworked to take in an array of property names? While this syntax might not be correct, I'm imagining it being used something like this:

interface WeakenedType extends Weaken<OriginalType, ['foo', 'bar']> {
    foo?: FooEnum;
    bar?: BarEnum;
}

Upvotes: 0

Views: 302

Answers (1)

Matt McCutchen
Matt McCutchen

Reputation: 30909

It looks like Weaken will do what you want if you pass it a union of the names of the properties you want to weaken, like this:

interface WeakenedType extends Weaken<OriginalType, 'foo' | 'bar'> {
    foo?: FooEnum;
    bar?: BarEnum;
}

Upvotes: 4

Related Questions