Created
August 9, 2023 02:40
-
-
Save imekachi/3cdaaaf89a6445103b1ffd1ea4c5bc2a to your computer and use it in GitHub Desktop.
TS utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type RequireAtLeastOne<T> = { | |
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Omit<T, K>> | |
}[keyof T] | |
/** | |
* This is to make Omit works correctly with union type. | |
* For example, | |
* type A = { | |
* key1: 'A' | |
* key2: string | |
* } | |
* type B = { | |
* key1: 'B' | |
* key2: string | |
* } | |
* type C = A | B | |
* | |
* // This wouldn't work | |
* type D = Omit<C, 'key2'> | |
* | |
* // We want to achieve something like this. | |
* // Distributively omit. | |
* type E = Omit<A, 'key2'> | Omit<B, 'key2'> | |
* | |
* // doc: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types | |
* // Note: this doesn't seem to work without the use of "any". | |
*/ | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
export type DistributiveOmit<T, K extends keyof any> = T extends any | |
? Omit<T, K> | |
: never | |
export type ExtractIterableType<T> = T extends Iterable<infer U> ? U : never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment