]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/core-utils/common/types.ts
Cleanup types dist before compilation
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / types.ts
1 /* eslint-disable @typescript-eslint/array-type */
2
3 export type FunctionPropertyNames<T> = {
4 [K in keyof T]: T[K] extends Function ? K : never
5 }[keyof T]
6
7 export type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>
8
9 export type AttributesOnly<T> = {
10 [K in keyof T]: T[K] extends Function ? never : T[K]
11 }
12
13 export type PickWith<T, KT extends keyof T, V> = {
14 [P in KT]: T[P] extends V ? V : never
15 }
16
17 export type PickWithOpt<T, KT extends keyof T, V> = {
18 [P in KT]?: T[P] extends V ? V : never
19 }
20
21 // https://github.com/krzkaczor/ts-essentials Rocks!
22 export type DeepPartial<T> = {
23 [P in keyof T]?: T[P] extends Array<infer U>
24 ? Array<DeepPartial<U>>
25 : T[P] extends ReadonlyArray<infer U>
26 ? ReadonlyArray<DeepPartial<U>>
27 : DeepPartial<T[P]>
28 }
29
30 type Primitive = string | Function | number | boolean | Symbol | undefined | null
31 export type DeepOmitHelper<T, K extends keyof T> = {
32 [P in K]: // extra level of indirection needed to trigger homomorhic behavior
33 T[P] extends infer TP // distribute over unions
34 ? TP extends Primitive
35 ? TP // leave primitives and functions alone
36 : TP extends any[]
37 ? DeepOmitArray<TP, K> // Array special handling
38 : DeepOmit<TP, K>
39 : never
40 }
41 export type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>>
42
43 export type DeepOmitArray<T extends any[], K> = {
44 [P in keyof T]: DeepOmit<T[P], K>
45 }