persona-community-5/.pnpm-store/v3/files/21/b069fc73ec383533e008e34341b09ce34be47f2a8fbce75f7d8a0ecf784320b92c09c5e69412de485f9f357e404838714dc4ff1dbc8efcd5cfbeeae715c76b
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

52 lines
1.3 KiB
Plaintext

/**
Create a union of types that share a common discriminant property.
Use-case: A shorter way to declare tagged unions with multiple members.
@example
```
import type {TaggedUnion} from 'type-fest';
type Tagged<Fields extends Record<string, unknown> = TaggedUnion<'type', Fields>
// The TaggedUnion utility reduces the amount of boilerplate needed to create a tagged union with multiple members, making the code more concise.
type EventMessage = Tagged<{
OpenExternalUrl: {
url: string;
id: number;
language: string;
};
ToggleBackButtonVisibility: {
visible: boolean;
};
PurchaseButtonPressed: {
price: number;
time: Date;
};
NavigationStateChanged: {
navigation?: string;
};
}>;
// Here is the same type created without this utility.
type EventMessage =
| {
type: 'OpenExternalUrl';
url: string;
id: number;
language: string;
}
| {type: 'ToggleBackButtonVisibility'; visible: boolean}
| {type: 'PurchaseButtonPressed'; price: number; time: Date}
| {type: 'NavigationStateChanged'; navigation?: string};
```
@category Utilities
*/
export type TaggedUnion<
TagKey extends string,
UnionMembers extends Record<string, Record<string, unknown>>,
> = {
[Name in keyof UnionMembers]: {[Key in TagKey]: Name} & UnionMembers[Name];
}[keyof UnionMembers];