type-fest/source/shared-union-fields.d.ts
Rebecca Stevens 9aba4c33c8
Add AllUnionFields type (#997)
Co-authored-by: Som Shekhar Mukherjee <iamssmkhrj@gmail.com>
2025-02-09 15:57:44 +07:00

77 lines
1.9 KiB
TypeScript

import type {NonRecursiveType, IsUnion} from './internal';
import type {IsNever} from './is-never';
import type {Simplify} from './simplify';
import type {UnknownArray} from './unknown-array';
/**
Create a type with shared fields from a union of object types.
Use-cases:
- You want a safe object type where each key exists in the union object.
- You want to focus on the common fields of the union type and don't want to have to care about the other fields.
@example
```
import type {SharedUnionFields} from 'type-fest';
type Cat = {
name: string;
type: 'cat';
catType: string;
};
type Dog = {
name: string;
type: 'dog';
dogType: string;
};
function displayPetInfo(petInfo: Cat | Dog) {
// typeof petInfo =>
// {
// name: string;
// type: 'cat';
// catType: string; // Needn't care about this field, because it's not a common pet info field.
// } | {
// name: string;
// type: 'dog';
// dogType: string; // Needn't care about this field, because it's not a common pet info field.
// }
// petInfo type is complex and have some needless fields
console.log('name: ', petInfo.name);
console.log('type: ', petInfo.type);
}
function displayPetInfo(petInfo: SharedUnionFields<Cat | Dog>) {
// typeof petInfo =>
// {
// name: string;
// type: 'cat' | 'dog';
// }
// petInfo type is simple and clear
console.log('name: ', petInfo.name);
console.log('type: ', petInfo.type);
}
```
@see SharedUnionFieldsDeep
@see AllUnionFields
@category Object
@category Union
*/
export type SharedUnionFields<Union> =
Extract<Union, NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray> extends infer SkippedMembers
? Exclude<Union, SkippedMembers> extends infer RelevantMembers
?
| SkippedMembers
| (IsNever<RelevantMembers> extends true
? never
: Simplify<Pick<RelevantMembers, keyof RelevantMembers>>)
: never
: never;