You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A TypeScript utility for destructuring discriminated unions while maintaining type safety. This package is tiny (no dependencies) and is niche but simple to use and solves a common problem in Typescript. Broadly, this package helps with:
7
+
A TypeScript utility for destructuring discriminated unions while maintaining type safety. This package is tiny (no dependencies) and solves a common problem in Typescript. Broadly, this package helps with:
8
8
- Safely destructuring discriminated unions
9
9
- Making switch/case statements exhaustive
10
10
- Improving state management patterns
@@ -67,17 +67,9 @@ type RequestState = DiscriminatedUnion<
If you want to define your keys elsewhere, you can provide them to the Discriminated Union and the TS complier will give you errors if some variant are not supplied in the list. For example, the following example will have a type error because the error variant is not specified.
* Creates a safely destructurable discriminated union type that maintains type safety.
48
-
*
49
-
* @template TDiscriminator - The string literal type that will be used as the discriminator property name
50
-
* @template TStates - An object type mapping discriminator values to their respective property types
69
+
* Merge two object types so that properties present in both objects become a union of their types and properties exclusive to one side are kept optional.
51
70
*
52
71
* @example
53
-
* type RequestState = SafeUnion<"status", {
54
-
* loading: { progress: number }; // Will have status: "loading"
55
-
* success: { data: unknown }; // Will have status: "success"
56
-
* error: { error: Error }; // Will have status: "error"
0 commit comments