ray-template/src/types/type-utils.ts
2023-03-19 21:02:36 +08:00

19 lines
681 B
TypeScript

export type ConditionalKeys<Base, Condition> = NonNullable<
// Wrap in `NonNullable` to strip away the `undefined` type from the produced union.
{
// Map through all the keys of the given base type.
[Key in keyof Base]: Base[Key] extends Condition // Pick only keys with types extending the given `Condition` type.
? // Retain this key since the condition passes.
Key
: // Discard this key since the condition fails.
never
// Convert the produced object into a union type of the keys which passed the conditional test.
}[keyof Base]
>
export type ConditionalPick<Base, Condition> = Pick<
Base,
ConditionalKeys<Base, Condition>
>