aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/typescript-utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-17 09:29:23 +0100
committerChocobozzz <me@florianbigard.com>2021-12-17 09:29:23 +0100
commitbf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (patch)
tree54b40aaf01bae210632473285c3c7571d51e4f89 /shared/typescript-utils
parent6b5f72beda96d8b7e4d6329c4001827334de27dd (diff)
downloadPeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.gz
PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.zst
PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.zip
shared/ typescript types dir server-commands
Diffstat (limited to 'shared/typescript-utils')
-rw-r--r--shared/typescript-utils/index.ts1
-rw-r--r--shared/typescript-utils/types.ts45
2 files changed, 46 insertions, 0 deletions
diff --git a/shared/typescript-utils/index.ts b/shared/typescript-utils/index.ts
new file mode 100644
index 000000000..c9f6f047d
--- /dev/null
+++ b/shared/typescript-utils/index.ts
@@ -0,0 +1 @@
export * from './types'
diff --git a/shared/typescript-utils/types.ts b/shared/typescript-utils/types.ts
new file mode 100644
index 000000000..bd2a97b98
--- /dev/null
+++ b/shared/typescript-utils/types.ts
@@ -0,0 +1,45 @@
1/* eslint-disable @typescript-eslint/array-type */
2
3export type FunctionPropertyNames<T> = {
4 [K in keyof T]: T[K] extends Function ? K : never
5}[keyof T]
6
7export type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>
8
9export type AttributesOnly<T> = {
10 [K in keyof T]: T[K] extends Function ? never : T[K]
11}
12
13export type PickWith<T, KT extends keyof T, V> = {
14 [P in KT]: T[P] extends V ? V : never
15}
16
17export 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!
22export 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
30type Primitive = string | Function | number | boolean | Symbol | undefined | null
31export 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}
41export type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>>
42
43export type DeepOmitArray<T extends any[], K> = {
44 [P in keyof T]: DeepOmit<T[P], K>
45}