@planet-matrix/mobius-model 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +30 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +22 -4
- package/package.json +3 -3
- package/scripts/build.ts +4 -4
- package/src/basic/README.md +144 -0
- package/src/basic/array.ts +872 -0
- package/src/basic/bigint.ts +114 -0
- package/src/basic/boolean.ts +180 -0
- package/src/basic/enhance.ts +10 -0
- package/src/basic/error.ts +51 -0
- package/src/basic/function.ts +453 -0
- package/src/basic/helper.ts +276 -0
- package/src/basic/index.ts +17 -0
- package/src/basic/is.ts +320 -0
- package/src/basic/number.ts +178 -0
- package/src/basic/object.ts +140 -0
- package/src/basic/promise.ts +464 -0
- package/src/basic/regexp.ts +7 -0
- package/src/basic/stream.ts +140 -0
- package/src/basic/string.ts +308 -0
- package/src/basic/symbol.ts +164 -0
- package/src/basic/temporal.ts +224 -0
- package/src/encoding/README.md +105 -0
- package/src/encoding/base64.ts +98 -0
- package/src/encoding/index.ts +1 -0
- package/src/index.ts +4 -0
- package/src/random/README.md +109 -0
- package/src/random/index.ts +1 -0
- package/src/random/uuid.ts +103 -0
- package/src/type/README.md +330 -0
- package/src/type/array.ts +5 -0
- package/src/type/boolean.ts +471 -0
- package/src/type/class.ts +419 -0
- package/src/type/function.ts +1519 -0
- package/src/type/helper.ts +135 -0
- package/src/type/index.ts +14 -0
- package/src/type/intersection.ts +93 -0
- package/src/type/is.ts +247 -0
- package/src/type/iteration.ts +233 -0
- package/src/type/number.ts +732 -0
- package/src/type/object.ts +788 -0
- package/src/type/path.ts +73 -0
- package/src/type/string.ts +1004 -0
- package/src/type/tuple.ts +2424 -0
- package/src/type/union.ts +108 -0
- package/tests/unit/basic/array.spec.ts +290 -0
- package/tests/unit/basic/bigint.spec.ts +50 -0
- package/tests/unit/basic/boolean.spec.ts +74 -0
- package/tests/unit/basic/error.spec.ts +32 -0
- package/tests/unit/basic/function.spec.ts +175 -0
- package/tests/unit/basic/helper.spec.ts +118 -0
- package/tests/unit/basic/number.spec.ts +74 -0
- package/tests/unit/basic/object.spec.ts +46 -0
- package/tests/unit/basic/promise.spec.ts +232 -0
- package/tests/unit/basic/regexp.spec.ts +11 -0
- package/tests/unit/basic/stream.spec.ts +120 -0
- package/tests/unit/basic/string.spec.ts +74 -0
- package/tests/unit/basic/symbol.spec.ts +72 -0
- package/tests/unit/basic/temporal.spec.ts +78 -0
- package/tests/unit/encoding/base64.spec.ts +40 -0
- package/tests/unit/random/uuid.spec.ts +37 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/reactor/index.d.ts +0 -3
- package/dist/reactor/index.d.ts.map +0 -1
- package/dist/reactor/reactor-core/flags.d.ts +0 -99
- package/dist/reactor/reactor-core/flags.d.ts.map +0 -1
- package/dist/reactor/reactor-core/index.d.ts +0 -4
- package/dist/reactor/reactor-core/index.d.ts.map +0 -1
- package/dist/reactor/reactor-core/primitive.d.ts +0 -276
- package/dist/reactor/reactor-core/primitive.d.ts.map +0 -1
- package/dist/reactor/reactor-core/reactive-system.d.ts +0 -241
- package/dist/reactor/reactor-core/reactive-system.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/branch.d.ts +0 -19
- package/dist/reactor/reactor-operators/branch.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/convert.d.ts +0 -30
- package/dist/reactor/reactor-operators/convert.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/create.d.ts +0 -26
- package/dist/reactor/reactor-operators/create.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/filter.d.ts +0 -269
- package/dist/reactor/reactor-operators/filter.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/index.d.ts +0 -8
- package/dist/reactor/reactor-operators/index.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/join.d.ts +0 -48
- package/dist/reactor/reactor-operators/join.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/map.d.ts +0 -165
- package/dist/reactor/reactor-operators/map.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/utility.d.ts +0 -48
- package/dist/reactor/reactor-operators/utility.d.ts.map +0 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// oxlint-disable ban-types
|
|
2
|
+
// oxlint-disable no-explicit-any
|
|
3
|
+
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// Helpers
|
|
6
|
+
// ============================================================================
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @example
|
|
10
|
+
* ```
|
|
11
|
+
* // Expect: string | number | symbol
|
|
12
|
+
* type Example1 = KeyOfBase
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export type KeyOfBase = keyof any
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Cast type T to SafeT if possible; otherwise, return SafeT.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```
|
|
22
|
+
* // Expect: number
|
|
23
|
+
* type Example1 = Cast<42, number>;
|
|
24
|
+
* // Expect: string
|
|
25
|
+
* type Example2 = Cast<42, string>;
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export type Cast<T, SafeT> = T extends SafeT ? T : SafeT
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Select a type based on a boolean condition.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```
|
|
35
|
+
* // Expect: 1
|
|
36
|
+
* type Example1 = If<true, 1, 2>
|
|
37
|
+
* // Expect: 2
|
|
38
|
+
* type Example2 = If<false, 1, 2>
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export type If<T extends boolean, TRUE, FALSE = never> = T extends true ? TRUE : FALSE
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Return `T` if it extends `SafeT`, otherwise return `Catch`.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```
|
|
48
|
+
* // Expect: 42
|
|
49
|
+
* type Example1 = Try<42, number, 0>
|
|
50
|
+
* // Expect: 0
|
|
51
|
+
* type Example2 = Try<'x', number, 0>
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export type Try<T, SafeT, Catch = never> = T extends SafeT ? T : Catch
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get keys of an array/tuple or object, with numeric index support for tuples.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```
|
|
61
|
+
* // Expect: 0 | 1 | number
|
|
62
|
+
* type Example1 = Keys<[string, number]>
|
|
63
|
+
* // Expect: 'a' | 'b'
|
|
64
|
+
* type Example2 = Keys<{ a: 1; b: 2 }>
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export type Keys<A> = A extends readonly any[]
|
|
68
|
+
? Exclude<keyof A, keyof any[]> | number
|
|
69
|
+
: keyof A
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if two types are exactly equal.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```
|
|
76
|
+
* // Expect: true
|
|
77
|
+
* type Example1 = IsEqual<1, 1>
|
|
78
|
+
* // Expect: false
|
|
79
|
+
* type Example2 = IsEqual<1, number>
|
|
80
|
+
* ```
|
|
81
|
+
* WARNING: https://github.com/ts-essentials/ts-essentials/blob/5aa1f264e77fb36bb3f673c49f00927c7c181a7f/lib/types.ts#L440
|
|
82
|
+
* @see https://stackoverflow.com/a/52473108/1815209 with comments
|
|
83
|
+
* @see https://github.com/millsp/ts-toolbelt/blob/319e551/sources/Any/Equals.ts#L15
|
|
84
|
+
*/
|
|
85
|
+
export type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false
|
|
86
|
+
/**
|
|
87
|
+
* Select types based on exact type equality.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```
|
|
91
|
+
* // Expect: 'yes'
|
|
92
|
+
* type Example1 = IfEqual<1, 1, 'yes', 'no'>
|
|
93
|
+
* // Expect: 'no'
|
|
94
|
+
* type Example2 = IfEqual<1, number, 'yes', 'no'>
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export type IfEqual<X, Y, TRUE = true, FALSE = false> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? TRUE : FALSE
|
|
98
|
+
|
|
99
|
+
declare const InternalBrandId: unique symbol
|
|
100
|
+
/**
|
|
101
|
+
* Define nominal type of U based on type of T. Similar to Opaque types in Flow.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```
|
|
105
|
+
* type USD = Brand<number, "USD">
|
|
106
|
+
* type EUR = Brand<number, "EUR">
|
|
107
|
+
*
|
|
108
|
+
* const tax = 5 as USD;
|
|
109
|
+
* const usd = 10 as USD;
|
|
110
|
+
* const eur = 10 as EUR;
|
|
111
|
+
*
|
|
112
|
+
* function gross(net: USD): USD {
|
|
113
|
+
* return (net + tax) as USD;
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* // Expect: No compile error
|
|
117
|
+
* gross(usd);
|
|
118
|
+
* // Expect: Compile error (Type '"EUR"' is not assignable to type '"USD"'.)
|
|
119
|
+
* gross(eur);
|
|
120
|
+
* ```
|
|
121
|
+
* @see {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts}
|
|
122
|
+
*/
|
|
123
|
+
export type Brand<T, U> = T & { [InternalBrandId]: U }
|
|
124
|
+
|
|
125
|
+
declare const InternalXPlaceholder: unique symbol
|
|
126
|
+
/**
|
|
127
|
+
* Provide an opaque placeholder type for generics.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```
|
|
131
|
+
* // Expect: X
|
|
132
|
+
* type Example1 = X
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export type X = typeof InternalXPlaceholder & {}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./helper.ts"
|
|
2
|
+
export * from "./is.ts"
|
|
3
|
+
export * from "./union.ts"
|
|
4
|
+
export * from "./intersection.ts"
|
|
5
|
+
export * from "./string.ts"
|
|
6
|
+
export * from "./path.ts"
|
|
7
|
+
export * from "./boolean.ts"
|
|
8
|
+
export * from "./number.ts"
|
|
9
|
+
export * from "./object.ts"
|
|
10
|
+
export * from "./function.ts"
|
|
11
|
+
export * from "./tuple.ts"
|
|
12
|
+
export * from "./array.ts"
|
|
13
|
+
export * from "./class.ts"
|
|
14
|
+
export * from "./iteration.ts"
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { UnionToIntersection, UnionToTuple } from "./union.ts"
|
|
2
|
+
|
|
3
|
+
// ============================================================================
|
|
4
|
+
// Generation
|
|
5
|
+
// ============================================================================
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Create an intersection from a tuple of types.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```
|
|
12
|
+
* // Expect: { a: 1 } & { b: 2 }
|
|
13
|
+
* type Example1 = IntersectionAll<[{ a: 1 }, { b: 2 }]>
|
|
14
|
+
* // Expect: string & number
|
|
15
|
+
* type Example2 = IntersectionAll<[string, number]>
|
|
16
|
+
* // Expect: unknown
|
|
17
|
+
* type Example3 = IntersectionAll<[]>
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type IntersectionAll<T extends readonly unknown[], Fallback = unknown> =
|
|
21
|
+
[T[number]] extends [never] ? Fallback : UnionToIntersection<T[number]>
|
|
22
|
+
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// Manipulation
|
|
25
|
+
// ============================================================================
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Merge an intersection of object types into a single object type.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```
|
|
32
|
+
* // Expect: { a: 1; b: 2 }
|
|
33
|
+
* type Example1 = IntersectionMerge<{ a: 1 } & { b: 2 }>
|
|
34
|
+
* // Expect: { a: string & number }
|
|
35
|
+
* type Example2 = IntersectionMerge<{ a: string } & { a: number }>
|
|
36
|
+
* // Expect: {}
|
|
37
|
+
* type Example3 = IntersectionMerge<{}>
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export type IntersectionMerge<T extends Record<PropertyKey, unknown>> = {
|
|
41
|
+
[Key in keyof T]: T[Key]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Prettify an intersection of object types.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```
|
|
49
|
+
* // Expect: { id: string; active: boolean }
|
|
50
|
+
* type Example1 = IntersectionPrettify<{ id: string } & { active: boolean }>
|
|
51
|
+
* // Expect: { value: number }
|
|
52
|
+
* type Example2 = IntersectionPrettify<{ value: number }>
|
|
53
|
+
* // Expect: {}
|
|
54
|
+
* type Example3 = IntersectionPrettify<{}>
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export type IntersectionPrettify<T extends Record<PropertyKey, unknown>> = IntersectionMerge<T>
|
|
58
|
+
|
|
59
|
+
// ============================================================================
|
|
60
|
+
// Conversion
|
|
61
|
+
// ============================================================================
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Convert an intersection of object types into a union of object types.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```
|
|
68
|
+
* // Expect: { a: string } | { b: number }
|
|
69
|
+
* type Example1 = ObjectIntersectionToUnion<{ a: string } & { b: number }>
|
|
70
|
+
* // Expect: { a: never }
|
|
71
|
+
* type Example2 = ObjectIntersectionToUnion<{ a: string } & { a: number }>
|
|
72
|
+
* // Expect: never
|
|
73
|
+
* type Example3 = ObjectIntersectionToUnion<{}>
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export type ObjectIntersectionToUnion<T> = {
|
|
77
|
+
[K in keyof T]: { [P in K]: T[P] }
|
|
78
|
+
}[keyof T]
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Convert an intersection of object types into a tuple of object types.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```
|
|
85
|
+
* // Expect: [{ a: string }, { b: number }]
|
|
86
|
+
* type Example1 = ObjectIntersectionToTuple<{ a: string } & { b: number }>
|
|
87
|
+
* // Expect: [{ a: never }]
|
|
88
|
+
* type Example2 = ObjectIntersectionToTuple<{ a: string } & { a: number }>
|
|
89
|
+
* // Expect: []
|
|
90
|
+
* type Example3 = ObjectIntersectionToTuple<{}>
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export type ObjectIntersectionToTuple<T> = UnionToTuple<ObjectIntersectionToUnion<T>>
|
package/src/type/is.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// oxlint-disable no-explicit-any
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types}
|
|
5
|
+
*/
|
|
6
|
+
export type Primitive = string | number | boolean | bigint | symbol | null | undefined
|
|
7
|
+
/**
|
|
8
|
+
* @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#object-type}
|
|
9
|
+
*/
|
|
10
|
+
export type NonPrimitive = object
|
|
11
|
+
|
|
12
|
+
export type IsPrimitive<T> =
|
|
13
|
+
T extends string ? true : T extends number ? true : T extends boolean ? true : T extends bigint ? true
|
|
14
|
+
: T extends symbol ? true : T extends null ? true : T extends undefined ? true : false
|
|
15
|
+
export type IfPrimitive<T, TRUE = true, FALSE = never> = IsPrimitive<T> extends true ? TRUE : FALSE
|
|
16
|
+
|
|
17
|
+
export type IsPrimitiveUnion<T> = T extends Primitive ? true : false
|
|
18
|
+
export type IfPrimitiveUnion<T, TRUE = true, FALSE = never> = IsPrimitiveUnion<T> extends true ? TRUE : FALSE
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Predicate whether the target is `string`.
|
|
22
|
+
*/
|
|
23
|
+
export type IsString<T> = T extends string ? true : false
|
|
24
|
+
export type IfString<T, TRUE = true, FALSE = never> = IsString<T> extends true ? TRUE : FALSE
|
|
25
|
+
export type CastString<T> = T extends string ? T : string
|
|
26
|
+
export type IsStringLiteral<T> = T extends string ? (string extends T ? false : true) : false
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Predicate whether the target is `number`.
|
|
30
|
+
*/
|
|
31
|
+
export type IsNumber<T> = T extends number ? true : false
|
|
32
|
+
export type IfNumber<T, TRUE = true, FALSE = never> = IsNumber<T> extends true ? TRUE : FALSE
|
|
33
|
+
export type CastNumber<T> = T extends number ? T : number
|
|
34
|
+
export type IsNumberLiteral<T> = T extends number ? (number extends T ? false : true) : false
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Predicate whether the target is `boolean`.
|
|
38
|
+
*/
|
|
39
|
+
export type IsBoolean<T> = T extends boolean ? true : false
|
|
40
|
+
export type IfBoolean<T, TRUE = true, FALSE = never> = IsBoolean<T> extends true ? TRUE : FALSE
|
|
41
|
+
export type CastBoolean<T> = T extends boolean ? T : boolean
|
|
42
|
+
export type IsTrue<T> = T extends true ? true : false
|
|
43
|
+
export type IsFalse<T> = T extends false ? true : false
|
|
44
|
+
export type IsBooleanLiteral<T> =
|
|
45
|
+
[T] extends [boolean] // Check if T is assignable to boolean (true, false, or boolean)
|
|
46
|
+
? ([boolean] extends [T] // Check if boolean is assignable to T (only true for 'boolean' itself)
|
|
47
|
+
? false // If both are true, it's the general 'boolean' type
|
|
48
|
+
: true // Otherwise, it's a specific literal (true or false)
|
|
49
|
+
)
|
|
50
|
+
: false; // Not a boolean type at all
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Predicate whether the target is `bigint`.
|
|
54
|
+
*/
|
|
55
|
+
export type IsBigInt<T> = T extends bigint ? true : false
|
|
56
|
+
export type IfBigInt<T, TRUE = true, FALSE = never> = IsBigInt<T> extends true ? TRUE : FALSE
|
|
57
|
+
export type CastBigInt<T> = T extends bigint ? T : bigint
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Predicate whether the target is `symbol`.
|
|
61
|
+
*/
|
|
62
|
+
export type IsSymbol<T> = T extends symbol ? true : false
|
|
63
|
+
export type IfSymbol<T, TRUE = true, FALSE = never> = IsSymbol<T> extends true ? TRUE : FALSE
|
|
64
|
+
export type CastSymbol<T> = T extends symbol ? T : symbol
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Predicate whether the target is `null`.
|
|
68
|
+
*/
|
|
69
|
+
export type IsNull<T> = Exclude<T, null> extends never ? true : false
|
|
70
|
+
export type IfNull<T, TRUE = true, FALSE = never> = IsNull<T> extends true ? TRUE : FALSE
|
|
71
|
+
export type CastNull<T> = T extends null ? T : null
|
|
72
|
+
export type IsNullable<T> = T extends Exclude<T, null> ? false : true
|
|
73
|
+
export type IfNullable<T, TRUE = true, FALSE = never> = IsNullable<T> extends true ? TRUE : FALSE
|
|
74
|
+
export type Nullable<T> = T | null
|
|
75
|
+
export type NonNullable<T> = Exclude<T, null>
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Predicate whether the target is `undefined`.
|
|
79
|
+
*/
|
|
80
|
+
export type IsUndefined<T> = Exclude<T, undefined> extends never ? true : false
|
|
81
|
+
export type IfUndefined<T, TRUE = true, FALSE = never> = IsUndefined<T> extends true ? TRUE : FALSE
|
|
82
|
+
export type CastUndefined<T> = T extends undefined ? T : undefined
|
|
83
|
+
export type IsUndefinedable<T> = T extends Exclude<T, undefined> ? false : true
|
|
84
|
+
export type IfUndefinedable<T, TRUE = true, FALSE = never> = IsUndefinedable<T> extends true ? TRUE : FALSE
|
|
85
|
+
export type Undefinedable<T> = T | undefined
|
|
86
|
+
export type NonUndefinedable<T> = Exclude<T, undefined>
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Predicate whether the target is `null | undefined`.
|
|
90
|
+
*/
|
|
91
|
+
export type IsNil<T> = Exclude<T, undefined | null> extends never ? true : false
|
|
92
|
+
export type IfNil<T, TRUE = true, FALSE = never> = IsNil<T> extends true ? TRUE : FALSE
|
|
93
|
+
export type CastNil<T> = T extends undefined | null ? T : undefined | null
|
|
94
|
+
export type IsNilable<T> = T extends Exclude<T, null | undefined> ? false : true
|
|
95
|
+
export type IfNilable<T, TRUE = true, FALSE = never> = IsNilable<T> extends true ? TRUE : FALSE
|
|
96
|
+
export type Nilable<T> = T | null | undefined
|
|
97
|
+
export type NonNilable<T> = Exclude<T, null | undefined>
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Predicate whether the target is `Record`.
|
|
101
|
+
*/
|
|
102
|
+
export type IsRecord<T> = T extends Record<string | number | symbol, unknown> ? true : false
|
|
103
|
+
export type IfRecord<T, TRUE = true, FALSE = never> = IsRecord<T> extends true ? TRUE : FALSE
|
|
104
|
+
export type IsStringRecord<T> = T extends Record<string, unknown> ? true : false
|
|
105
|
+
export type IfStringRecord<T, TRUE = true, FALSE = never> = IsStringRecord<T> extends true ? TRUE : FALSE
|
|
106
|
+
export type IsNumberRecord<T> = T extends Record<number, unknown> ? true : false
|
|
107
|
+
export type IfNumberRecord<T, TRUE = true, FALSE = never> = IsNumberRecord<T> extends true ? TRUE : FALSE
|
|
108
|
+
export type IsSymbolRecord<T> = T extends Record<symbol, unknown> ? true : false
|
|
109
|
+
export type IfSymbolRecord<T, TRUE = true, FALSE = never> = IsSymbolRecord<T> extends true ? TRUE : FALSE
|
|
110
|
+
|
|
111
|
+
// oxlint-disable-next-line no-wrapper-object-types ban-types
|
|
112
|
+
export type IsObject<Target> = Target extends Object ? true : false
|
|
113
|
+
export type IfObject<Target, TRUE = true, FALSE = never> = IsObject<Target> extends true ? TRUE : FALSE
|
|
114
|
+
export type PlainObject = Record<PropertyKey, unknown>
|
|
115
|
+
/**
|
|
116
|
+
* Predicate whether the target is `EmptyObject`(`{}`).
|
|
117
|
+
*/
|
|
118
|
+
export type IsEmptyObject<Target extends Record<PropertyKey, unknown>> = keyof Target extends never ? true : false
|
|
119
|
+
export type IfEmptyObject<Target extends Record<PropertyKey, unknown>, TRUE = true, FALSE = never> = IsEmptyObject<Target> extends true ? TRUE : FALSE
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Predicate whether the target is `Array`.
|
|
123
|
+
*/
|
|
124
|
+
export type IsArray<T> = T extends readonly unknown[] ? (number extends T["length"] ? true : false) : false
|
|
125
|
+
export type IfArray<T, TRUE = true, FALSE = never> = IsArray<T> extends true ? TRUE : FALSE
|
|
126
|
+
// ReadonlyArray is subset of Array.
|
|
127
|
+
export type IsReadonlyArray<T> = T extends readonly unknown[] ? (T extends unknown[] ? false : true) : false
|
|
128
|
+
export type IfReadonlyArray<T, TRUE = true, FALSE = never> = IsReadonlyArray<T> extends true ? TRUE : FALSE
|
|
129
|
+
export type IsWritableArray<T> = T extends unknown[] ? (number extends T["length"] ? true : false) : false
|
|
130
|
+
export type IfWritableArray<T, TRUE = true, FALSE = never> = IsWritableArray<T> extends true ? TRUE : FALSE
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Predicate whether the target is `ArrayLike`.
|
|
134
|
+
*/
|
|
135
|
+
export type IsArrayLike<T> = T extends ArrayLike<unknown> ? true : false
|
|
136
|
+
export type IfArrayLike<T, TRUE = true, FALSE = never> = IsArrayLike<T> extends true ? TRUE : FALSE
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Predicate whether the target is `Tuple`.
|
|
140
|
+
*
|
|
141
|
+
* @see {@link https://github.com/type-challenges/type-challenges/issues/4491#issuecomment-975808109}
|
|
142
|
+
*/
|
|
143
|
+
export type IsTuple<T> = T extends readonly unknown[] ? (number extends T["length"] ? false : true) : false
|
|
144
|
+
export type IfTuple<T, TRUE = true, FALSE = never> = IsTuple<T> extends true ? TRUE : FALSE
|
|
145
|
+
export type IsReadonlyTuple<T> = T extends readonly unknown[] ? (T extends unknown[] ? false : true) : false
|
|
146
|
+
export type IfReadonlyTuple<T, TRUE = true, FALSE = never> = IsReadonlyTuple<T> extends true ? TRUE : FALSE
|
|
147
|
+
export type IsWritableTuple<T> = T extends unknown[] ? (number extends T["length"] ? false : true) : false
|
|
148
|
+
export type IfWritableTuple<T, TRUE = true, FALSE = never> = IsWritableTuple<T> extends true ? TRUE : FALSE
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Predicate whether the target is `Array` or `Tuple`.
|
|
152
|
+
*/
|
|
153
|
+
export type IsArrayOrTuple<T> = T extends readonly unknown[] ? true : false
|
|
154
|
+
export type IfArrayOrTuple<T, TRUE = true, FALSE = never> = IsArrayOrTuple<T> extends true ? TRUE : FALSE
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Predicate whether the target is of type function.
|
|
158
|
+
*/
|
|
159
|
+
export type AnyFunction = (...args: any[]) => any
|
|
160
|
+
export type AnyFunctionOfTake<A> = A extends readonly [...infer R] ? ((...args: R) => any) : ((arg: A) => any)
|
|
161
|
+
export type AnyFunctionOfReturn<T> = (...args: any[]) => T
|
|
162
|
+
export type IsFunction<T> = T extends AnyFunction ? true : false
|
|
163
|
+
export type IfFunction<T, TRUE = true, FALSE = never> = IsFunction<T> extends true ? TRUE : FALSE
|
|
164
|
+
export type CastFunction<T> = T extends AnyFunction ? T : AnyFunction
|
|
165
|
+
|
|
166
|
+
export type AnySyncFunction = (...args: any[]) => Exclude<unknown, Promise<any>>;
|
|
167
|
+
/**
|
|
168
|
+
* Predicate whether the target is of type sync function.
|
|
169
|
+
*/
|
|
170
|
+
export type IsSyncFunction<T> = T extends (...args: any[]) => infer R ? (R extends Promise<any> ? false : true) : false
|
|
171
|
+
export type IfSyncFunction<T, TRUE = true, FALSE = never> = IsSyncFunction<T> extends true ? TRUE : FALSE
|
|
172
|
+
export type CastSyncFunction<T> = T extends (...args: unknown[]) => infer R ? (R extends Promise<unknown> ? (...args: Parameters<T>) => Awaited<R> : T) : AnyFunction
|
|
173
|
+
|
|
174
|
+
export type AnyAsyncFunction = (...args: any[]) => Promise<any>
|
|
175
|
+
/**
|
|
176
|
+
* Predicate whether the target is of type async function.
|
|
177
|
+
*/
|
|
178
|
+
export type IsAsyncFunction<T> = T extends AnyAsyncFunction ? true : false
|
|
179
|
+
export type IfAsyncFunction<T, TRUE = true, FALSE = never> = IsAsyncFunction<T> extends true ? TRUE : FALSE
|
|
180
|
+
export type CastAsyncFunction<T> = T extends AnyAsyncFunction ? T : AnyAsyncFunction
|
|
181
|
+
|
|
182
|
+
export type AnyGeneratorFunction = (...args: any[]) => Generator<any, any, any>
|
|
183
|
+
/**
|
|
184
|
+
* Predicate whether the target is of type generator function.
|
|
185
|
+
*/
|
|
186
|
+
export type IsGeneratorFunction<T> = T extends AnyGeneratorFunction ? true : false
|
|
187
|
+
export type IfGeneratorFunction<T, TRUE = true, FALSE = never> = IsGeneratorFunction<T> extends true ? TRUE : FALSE
|
|
188
|
+
export type CastGeneratorFunction<T> = T extends AnyGeneratorFunction ? T : AnyGeneratorFunction
|
|
189
|
+
|
|
190
|
+
export type AnyAsyncGeneratorFunction = (...args: any[]) => AsyncGenerator<any, any, any>
|
|
191
|
+
/**
|
|
192
|
+
* Predicate whether the target is of type async generator function.
|
|
193
|
+
*/
|
|
194
|
+
export type IsAsyncGeneratorFunction<T> = T extends AnyAsyncGeneratorFunction ? true : false
|
|
195
|
+
export type IfAsyncGeneratorFunction<T, TRUE = true, FALSE = never> = IsAsyncGeneratorFunction<T> extends true ? TRUE : FALSE
|
|
196
|
+
export type CastAsyncGeneratorFunction<T> = T extends AnyAsyncGeneratorFunction ? T : AnyAsyncGeneratorFunction
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Predicate whether the target is `never`.
|
|
200
|
+
*/
|
|
201
|
+
export type IsNever<T> = [T] extends [never] ? true : false
|
|
202
|
+
export type IfNever<T, TRUE = true, FALSE = never> = IsNever<T> extends true ? TRUE : FALSE
|
|
203
|
+
export type CastNever<T> = T extends never ? T : never
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Predicate whether the target is `any`.
|
|
207
|
+
* @see {@link https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360}
|
|
208
|
+
*/
|
|
209
|
+
export type IsAny<T> = 0 extends (1 & T) ? true : false
|
|
210
|
+
export type IfAny<T, TRUE = true, FALSE = never> = IsAny<T> extends true ? TRUE : FALSE
|
|
211
|
+
export type CastAny<T> = T extends any ? T : any
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Predicate whether the target is `unknown`.
|
|
215
|
+
* @see {@link https://www.typescriptlang.org/docs/handbook/type-compatibility.html#any-unknown-object-void-undefined-null-and-never-assignability}
|
|
216
|
+
*/
|
|
217
|
+
export type IsUnknown<T> = IsNever<T> extends true
|
|
218
|
+
? false
|
|
219
|
+
: (
|
|
220
|
+
T extends unknown
|
|
221
|
+
? (
|
|
222
|
+
unknown extends T
|
|
223
|
+
? (IsAny<T> extends true ? false : true)
|
|
224
|
+
: false
|
|
225
|
+
)
|
|
226
|
+
: false
|
|
227
|
+
)
|
|
228
|
+
export type IfUnknown<T, TRUE = true, FALSE = never> = IsUnknown<T> extends true ? TRUE : FALSE
|
|
229
|
+
export type CastUnknown<T> = T extends unknown ? T : unknown
|
|
230
|
+
|
|
231
|
+
type InternalIsUnion<T, U> =
|
|
232
|
+
[T] extends [never]
|
|
233
|
+
? false
|
|
234
|
+
: (
|
|
235
|
+
T extends any
|
|
236
|
+
? ([U] extends [T] ? false : true)
|
|
237
|
+
: false
|
|
238
|
+
)
|
|
239
|
+
/**
|
|
240
|
+
* Predicate whether the target is `Union` type.
|
|
241
|
+
*
|
|
242
|
+
* @see {@link https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types}
|
|
243
|
+
*/
|
|
244
|
+
export type IsUnion<T> = InternalIsUnion<T, T>;
|
|
245
|
+
export type IfUnion<T, TRUE = true, FALSE = never> = IsUnion<T> extends true ? TRUE : FALSE
|
|
246
|
+
|
|
247
|
+
export interface EmptyInterface { }
|