Merge pull request #3435 from alfonso-salces/MOBILE-4081

MOBILE-4081 types: Create OmitUnion utility type
main
Noel De Martin 2022-11-10 08:58:53 +01:00 committed by GitHub
commit d0e3ad6ee8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -16,3 +16,32 @@
* Helper type to flatten complex types. * Helper type to flatten complex types.
*/ */
export type Pretty<T> = T extends infer U ? {[K in keyof U]: U[K]} : never; export type Pretty<T> = T extends infer U ? {[K in keyof U]: U[K]} : never;
/**
* Helper type to omit union.
* You can use it if need to omit an element from types union.
* If you omit a value in an union with `Omit<TypeUnion, 'value'>` you will obtain
* the values which are present in both types.
* For example, you have 3 types:
*
* ```
* type TypeA = { propA: boolean; propB: string; propToOmit: boolean }
* type TypeB = { propA: boolean; propToOmit: boolean }
* type TypeUnion = TypeA | TypeB
* ```
*
* @example
* ```
* type Result = Omit<TypeUnion, 'propToOmit'>;
* // ^? type Result = { propA: boolean };
* ```
*
* @example
* ```
* type Result = OmitUnion<TypeUnion, 'propToOmit'>;
* // ^? type Result = { propA: boolean, propB: string } | { propA: boolean }
* ```
*
* @see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
*/
export type OmitUnion<T, A extends keyof T> = T extends '' ? never : Omit<T, A>;