1

I would like to build a string union of all the required keys of a type. Example:

interface IPerson {
    readonly name: string;
    age?: number;
    weight: number;
}

RequiredKeys<IPerson>  // a type returning "name" | "weight"
ReadonlyKeys<IPerson>  // a type returning "name"

I can't figure out how to filter out optional (or readonly) keys

Jo VdB
  • 2,016
  • 18
  • 16

2 Answers2

1

TypeScript doesn't have a built in method yet to extract optionals.

interface IPerson {
  readonly name: string;
  age?: number;
  weight: number;
}

// First get the optional keys
type Optional<T> = {
  [K in keyof T]-?: ({} extends { [P in K]: T[K] } ? K : never)
}[keyof T];

// Use the pick to select them from the rest of the interface
const optionalPerson: Pick<IPerson, Optional<IPerson>> = {
  age: 2
};
makeitmorehuman
  • 11,287
  • 3
  • 52
  • 76
0

Thanks @ali-habibzadeh

type RequiredKeys<T> = {
  [K in keyof T]-?: ({} extends { [P in K]: T[K] } ? never : K)
}[keyof T];

type OptionalKeys<T> = {
  [K in keyof T]-?: ({} extends { [P in K]: T[K] } ? K : never)
}[keyof T];

To get the readonly/writable keys you can use: Details

type IfEquals<X, Y, A=X, B=never> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? A : B;

type WritableKeys<T> = {
  [P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, P>
}[keyof T];

type ReadonlyKeys<T> = {
  [P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>
}[keyof T];
Jo VdB
  • 2,016
  • 18
  • 16