-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
229 lines (202 loc) · 6.92 KB
/
index.ts
File metadata and controls
229 lines (202 loc) · 6.92 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import native from './native'
export type ParseSafe<T> = { success: boolean } & (
| { success: true; data: T }
| { success: false; reason: string }
)
export const {
BUndefined,
BNull,
BBoolean,
BNumber,
BString,
BArray,
BObject,
BUnion,
BTuple,
} = native
type BUndefined<T> = native.BUndefined<T>
type BNull<T> = native.BNull<T>
type BBoolean<T> = native.BBoolean<T>
type BNumber<T> = native.BNumber<T>
type BString<T> = native.BString<T>
type BArray<T> = native.BArray<T>
type BObject<T> = native.BObject<T>
type BUnion<T> = native.BUnion<T>
type BTuple<T> = native.BTuple<T>
export type BValue<T = unknown> =
| BUndefined<T>
| BNull<T>
| BBoolean<T>
| BNumber<T>
| BString<T>
| BArray<T>
| BObject<T>
| BUnion<T>
| BTuple<T>
/**
* Infer the `parse()` return type of a BValue schema
*/
export type InferParse<T extends BValue> = ReturnType<T['parse']>
/**
* Infer the `parseSafe()` return type of a BValue schema
*/
export type InferParseSafe<T extends BValue> = ParseSafe<InferParse<T>>
type ArrayElement<ArrayType extends readonly unknown[]> =
ArrayType extends readonly (infer ElementType)[] ? ElementType : never
type InferBObjectParseType<T extends Record<string, BValue>> = {
[key in keyof T]: InferParse<T[key]>
}
type InferBTupleParseType<T extends readonly BValue[]> = {
[key in keyof T]: InferParse<T[key]>
}
const undefined = native.BUndefined.default
const Null = native.BNull.default
const boolean = native.BBoolean.default
const number = native.BNumber.default
const string = native.BString.default
const array = <T extends BValue>(schema: T) =>
native.BArray._fromWrapped(schema._toWrapped()) as BArray<InferParse<T>[]>
const object = <T extends Record<string, BValue>>(schema: T) =>
native.BObject.new(
Object.fromEntries(
Object.entries(schema).map(([k, v]): [string, native.BWrapped] => [
k,
v._toWrapped(),
])
)
) as BObject<InferBObjectParseType<T>>
const union = <T extends readonly BValue[]>(...schemas: T) =>
native.BUnion._fromWrapped(
schemas.map(schema => schema._toWrapped())
) as BUnion<InferParse<ArrayElement<T>>>
const tuple = <T extends readonly BValue[]>(...schemas: T) =>
native.BTuple._fromWrapped(
schemas.map(schema => schema._toWrapped())
) as BTuple<InferBTupleParseType<T>>
export default {
undefined,
Null,
boolean,
number,
string,
array,
object,
union,
tuple,
}
// type NativeBValue =
// | native.BUndefined
// | native.BNull
// | native.BBoolean
// | native.BNumber
// | native.BString
// | native.BArray
// | native.BObject
// | native.BUnion
// type OverwriteMethods<T, Overwrite> = Omit<T, keyof Overwrite> & Overwrite
// // Correct all special method definitions
// type CorrectMethods<T extends NativeBValue, R> = OverwriteMethods<
// T,
// {
// parse(value: unknown): R
// parseSafe(value: unknown): ParseSafe<R>
// optional(): CorrectMethods<T, R | undefined>
// nullable(): CorrectMethods<T, R | null>
// nullish(): CorrectMethods<T, R | null | undefined>
// required(): CorrectMethods<T, Exclude<R, undefined>>
// nonNullable(): CorrectMethods<T, Exclude<R, null>>
// }
// >
// // Replaces return types of methods that return an instance of the native type with the corrected type
// // type CorrectReturnTypes<T, NativeValue extends NativeBValue> = {
// // [key in keyof T]: T[key] extends (...args: any) => NativeValue
// // ? (...args: Parameters<T[key]>) => T
// // : T[key]
// // }
// // type CorrectBValueType<T extends NativeBValue, R> = CorrectReturnTypes<
// // CorrectMethods<T, R>,
// // T
// // >
// // export type BUndefined = CorrectBValueType<native.BUndefined, undefined>
// // export type BNull = CorrectBValueType<native.BNull, null>
// // export type BBoolean = CorrectBValueType<native.BBoolean, boolean>
// // export type BNumber = CorrectBValueType<native.BNumber, number>
// // export type BString = CorrectBValueType<native.BString, string>
// // export type BArray<T> = CorrectBValueType<native.BArray, T[]>
// // export type BUnion<T> = CorrectBValueType<native.BUnion, T>
// // export type BObject<T> = CorrectBValueType<native.BObject, T>
// export type BUndefined = CorrectMethods<native.BUndefined, undefined>
// export type BNull = CorrectMethods<native.BNull, null>
// export type BBoolean = CorrectMethods<native.BBoolean, boolean>
// export type BNumber = CorrectMethods<native.BNumber, number>
// export type BString = CorrectMethods<native.BString, string>
// export type BArray<T> = CorrectMethods<native.BArray, T[]>
// export type BObject<T> = CorrectMethods<
// OverwriteMethods<
// native.BObject,
// {
// merge<O>(object: BObject<O>): BObject<Omit<T, keyof O> & O>
// }
// >,
// T
// >
// export type BUnion<T> = OverwriteMethods<
// CorrectMethods<native.BUnion, T>,
// { merge<U>(union: BUnion<U>): BUnion<T | U> }
// >
// export type BValue =
// | BUndefined
// | BNull
// | BBoolean
// | BNumber
// | BString
// | BArray<unknown>
// | BObject<unknown>
// | BUnion<unknown>
// export type ParseSafe<T> = { success: boolean } & (
// | { success: true; data: T }
// | { success: false; reason: string }
// )
// /**
// * Infer the `parse()` return type of a BValue schema
// */
// export type InferParse<T extends BValue> = ReturnType<T['parse']>
// /**
// * Infer the `parseSafe()` return type of a BValue schema
// */
// export type InferParseSafe<T extends BValue> = ParseSafe<InferParse<T>>
// type ArrayElement<ArrayType extends readonly unknown[]> =
// ArrayType extends readonly (infer ElementType)[] ? ElementType : never
// type InferBObjectParseType<T extends Record<string, BValue>> = {
// [key in keyof T]: InferParse<T[key]>
// }
// const undefined = native.BUndefined.default as () => BUndefined
// const Null = native.BNull.default as () => BNull
// const boolean = native.BBoolean.default as () => BBoolean
// const number = native.BNumber.default as () => BNumber
// const string = native.BString.default as () => BString
// const array = <T extends BValue>(schema: T) =>
// native.BArray._fromWrapped(schema._toWrapped()) as BArray<InferParse<T>>
// const union = <T extends readonly BValue[]>(...schemas: T) =>
// native.BUnion._fromWrapped(
// schemas.map(schema => schema._toWrapped())
// ) as BUnion<InferParse<ArrayElement<T>>>
// const object = <T extends Record<string, BValue>>(schema: T) =>
// native.BObject.new(
// Object.fromEntries(
// Object.entries(schema).map(([k, v]): [string, native.BWrapped] => [
// k,
// v._toWrapped(),
// ])
// )
// ) as BObject<InferBObjectParseType<T>>
// export default {
// undefined,
// Null,
// boolean,
// number,
// string,
// array,
// union,
// object,
// }