-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
118 lines (96 loc) · 3.16 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
let runAssertFns = true;
export function setConfig(options: {runAssertFns: boolean}) {
runAssertFns = options.runAssertFns;
}
export function assert(value: boolean, msg = `Assertion failed`): asserts value {
if (value !== true) {
throw new Error(msg);
}
}
export function assertType<T>(value: unknown): asserts value is T {}
export function assertFn(value: () => boolean, msg = `Assertion failed`) {
if (runAssertFns) {
if (value() !== true) {
throw new Error(msg);
}
}
}
export function ensureArray(value: unknown, msg = `Expected array but got ${value}`): asserts value is unknown[] {
if (!Array.isArray(value)) {
throw new Error(msg);
}
}
export function ensureObject<T extends object>(
value: unknown,
msg = `Expected object but got ${value}`,
): asserts value is T {
if (typeof value !== 'object' || value === null) {
throw new Error(msg);
}
}
export function ensureString(value: unknown, msg = `Expected string but got ${value}`): asserts value is string {
if (typeof value !== 'string') {
throw new Error(msg);
}
}
export function ensureNumber(value: unknown, msg = `Expected number but got ${value}`): asserts value is number {
if (typeof value !== 'number') {
throw new Error(msg);
}
}
export function ensureBoolean(value: unknown, msg = `Expected boolean but got ${value}`): asserts value is boolean {
if (typeof value !== 'boolean') {
throw new Error(msg);
}
}
export function ensureNonVoid<T>(value: T, msg = `Expected non undefined value`): asserts value is NonUndefined<T> {
if (value === undefined) {
throw new Error(msg);
}
}
export function ensureNonNull<T>(value: T, msg = `Expected non null value`): asserts value is NonNull<T> {
if (value === null) {
throw new Error(msg);
}
}
export function ensureNonNullable<T>(value: T, msg = `Expected non nullable value`): asserts value is NonNullable<T> {
if (value === undefined || value === null) {
throw new Error(msg);
}
}
export function nonVoid<T>(value: T, msg = `Expected non undefined value`) {
if (value === undefined) {
throw new Error(msg);
}
return value as NonUndefined<T>;
}
export function nonNull<T>(value: T, msg = `Expected non null value`) {
if (value === null) {
throw new Error(msg);
}
return value as NonNull<T>;
}
export function nonNullable<T>(value: T, msg = `Expected non nullable value`) {
if (value === null || value === undefined) {
throw new Error(msg);
}
return value as NonNullable<T>;
}
export function never(msg = `Never call`): never {
throw new Error(msg);
}
export function neverValue(value: never, msg = `Never possible value: ${JSON.stringify(value)}`): never {
throw new Error(msg);
}
export function castValue<T>(value: unknown): asserts value is T {}
export function cast<T>(value: unknown): T {
return value as T;
}
export function as<T>(value: T): T {
return value;
}
export function maybe<T>(val: T): T | undefined {
return val;
}
type NonUndefined<T> = T extends undefined ? never : T;
type NonNull<T> = T extends null ? never : T;