type-fest
Version:
A collection of essential TypeScript types
52 lines (40 loc) • 1.67 kB
TypeScript
import type {IsAny} from './is-any.d.ts';
/**
Extract all optional keys from the given type.
This is useful when you want to create a new type that contains different type values for the optional keys only.
@example
```
import type {OptionalKeysOf, Except} from 'type-fest';
type User = {
name: string;
surname: string;
luckyNumber?: number;
};
const REMOVE_FIELD = Symbol('remove field symbol');
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
};
const update1: UpdateOperation<User> = {
name: 'Alice',
};
const update2: UpdateOperation<User> = {
name: 'Bob',
luckyNumber: REMOVE_FIELD,
};
```
Known limitations:
- A union reports the keys of each of its members, so the result can name a key that is not a key of the union itself. `keyof` of a union is the intersection of its members' keys, so `OptionalKeysOf<{a?: string} | {b?: number}>` is `'a' | 'b'` while `keyof` of the same union is `never`. Read one member at a time if you need a key that is valid for the whole union.
@category Utilities
*/
export type OptionalKeysOf<Type extends object> =
// Check `any` once; each mapped key already belongs to the distributed member.
IsAny<Type> extends true ? never
: Type extends unknown // For distributing `Type`
? (keyof {[Key in keyof Type as
Type extends Record<Key, Type[Key]>
? never
: Key
]: never
}) & keyof Type // Intersect with `keyof Type` to narrow the member's own keys. For a union this runs per member, see the note on unions in the docs.
: never; // Should never happen
export {};