@oscarpalmer/jhunal 0.1.0 → 0.3.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/types/index.d.cts CHANGED
@@ -1,3 +1,349 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
+ /**
4
+ Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
5
+
6
+ Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
7
+
8
+ @example
9
+ ```
10
+ import type {UnionToIntersection} from 'type-fest';
11
+
12
+ type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
13
+
14
+ type Intersection = UnionToIntersection<Union>;
15
+ //=> {the(): void; great(arg: string): void; escape: boolean};
16
+ ```
17
+
18
+ A more applicable example which could make its way into your library code follows.
19
+
20
+ @example
21
+ ```
22
+ import type {UnionToIntersection} from 'type-fest';
23
+
24
+ class CommandOne {
25
+ commands: {
26
+ a1: () => undefined,
27
+ b1: () => undefined,
28
+ }
29
+ }
30
+
31
+ class CommandTwo {
32
+ commands: {
33
+ a2: (argA: string) => undefined,
34
+ b2: (argB: string) => undefined,
35
+ }
36
+ }
37
+
38
+ const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
39
+ type Union = typeof union;
40
+ //=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
41
+
42
+ type Intersection = UnionToIntersection<Union>;
43
+ //=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
44
+ ```
45
+
46
+ @category Type
47
+ */
48
+ export type UnionToIntersection<Union> = (
49
+ // `extends unknown` is always going to be the case and is used to convert the
50
+ // `Union` into a [distributive conditional
51
+ // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
52
+ Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
53
+ /**
54
+ Extract all optional keys from the given type.
55
+
56
+ This is useful when you want to create a new type that contains different type values for the optional keys only.
57
+
58
+ @example
59
+ ```
60
+ import type {OptionalKeysOf, Except} from 'type-fest';
61
+
62
+ interface User {
63
+ name: string;
64
+ surname: string;
65
+
66
+ luckyNumber?: number;
67
+ }
68
+
69
+ const REMOVE_FIELD = Symbol('remove field symbol');
70
+ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
71
+ [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
72
+ };
73
+
74
+ const update1: UpdateOperation<User> = {
75
+ name: 'Alice'
76
+ };
77
+
78
+ const update2: UpdateOperation<User> = {
79
+ name: 'Bob',
80
+ luckyNumber: REMOVE_FIELD
81
+ };
82
+ ```
83
+
84
+ @category Utilities
85
+ */
86
+ export type OptionalKeysOf<BaseType extends object> = BaseType extends unknown // For distributing `BaseType`
87
+ ? (keyof {
88
+ [Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never;
89
+ }) & (keyof BaseType) // Intersect with `keyof BaseType` to ensure result of `OptionalKeysOf<BaseType>` is always assignable to `keyof BaseType`
90
+ : never; // Should never happen
91
+ /**
92
+ Extract all required keys from the given type.
93
+
94
+ This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
95
+
96
+ @example
97
+ ```
98
+ import type {RequiredKeysOf} from 'type-fest';
99
+
100
+ declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
101
+
102
+ interface User {
103
+ name: string;
104
+ surname: string;
105
+
106
+ luckyNumber?: number;
107
+ }
108
+
109
+ const validator1 = createValidation<User>('name', value => value.length < 25);
110
+ const validator2 = createValidation<User>('surname', value => value.length < 25);
111
+ ```
112
+
113
+ @category Utilities
114
+ */
115
+ export type RequiredKeysOf<BaseType extends object> = BaseType extends unknown // For distributing `BaseType`
116
+ ? Exclude<keyof BaseType, OptionalKeysOf<BaseType>> : never; // Should never happen
117
+ /**
118
+ Returns a boolean for whether the given type is `never`.
119
+
120
+ @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
121
+ @link https://stackoverflow.com/a/53984913/10292952
122
+ @link https://www.zhenghao.io/posts/ts-never
123
+
124
+ Useful in type utilities, such as checking if something does not occur.
125
+
126
+ @example
127
+ ```
128
+ import type {IsNever, And} from 'type-fest';
129
+
130
+ // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
131
+ type AreStringsEqual<A extends string, B extends string> =
132
+ And<
133
+ IsNever<Exclude<A, B>> extends true ? true : false,
134
+ IsNever<Exclude<B, A>> extends true ? true : false
135
+ >;
136
+
137
+ type EndIfEqual<I extends string, O extends string> =
138
+ AreStringsEqual<I, O> extends true
139
+ ? never
140
+ : void;
141
+
142
+ function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
143
+ if (input === output) {
144
+ process.exit(0);
145
+ }
146
+ }
147
+
148
+ endIfEqual('abc', 'abc');
149
+ //=> never
150
+
151
+ endIfEqual('abc', '123');
152
+ //=> void
153
+ ```
154
+
155
+ @category Type Guard
156
+ @category Utilities
157
+ */
158
+ export type IsNever<T> = [
159
+ T
160
+ ] extends [
161
+ never
162
+ ] ? true : false;
163
+ /**
164
+ Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
165
+
166
+ @example
167
+ ```
168
+ import type {Simplify} from 'type-fest';
169
+
170
+ type PositionProps = {
171
+ top: number;
172
+ left: number;
173
+ };
174
+
175
+ type SizeProps = {
176
+ width: number;
177
+ height: number;
178
+ };
179
+
180
+ // In your editor, hovering over `Props` will show a flattened object with all the properties.
181
+ type Props = Simplify<PositionProps & SizeProps>;
182
+ ```
183
+
184
+ Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
185
+
186
+ If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
187
+
188
+ @example
189
+ ```
190
+ import type {Simplify} from 'type-fest';
191
+
192
+ interface SomeInterface {
193
+ foo: number;
194
+ bar?: string;
195
+ baz: number | undefined;
196
+ }
197
+
198
+ type SomeType = {
199
+ foo: number;
200
+ bar?: string;
201
+ baz: number | undefined;
202
+ };
203
+
204
+ const literal = {foo: 123, bar: 'hello', baz: 456};
205
+ const someType: SomeType = literal;
206
+ const someInterface: SomeInterface = literal;
207
+
208
+ function fn(object: Record<string, unknown>): void {}
209
+
210
+ fn(literal); // Good: literal object type is sealed
211
+ fn(someType); // Good: type is sealed
212
+ fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
213
+ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
214
+ ```
215
+
216
+ @link https://github.com/microsoft/TypeScript/issues/15300
217
+ @see SimplifyDeep
218
+ @category Object
219
+ */
220
+ export type Simplify<T> = {
221
+ [KeyType in keyof T]: T[KeyType];
222
+ } & {};
223
+ /**
224
+ Returns the last element of a union type.
225
+
226
+ @example
227
+ ```
228
+ type Last = LastOfUnion<1 | 2 | 3>;
229
+ //=> 3
230
+ ```
231
+ */
232
+ export type LastOfUnion<T> = UnionToIntersection<T extends any ? () => T : never> extends () => (infer R) ? R : never;
233
+ /**
234
+ Convert a union type into an unordered tuple type of its elements.
235
+
236
+ "Unordered" means the elements of the tuple are not guaranteed to be in the same order as in the union type. The arrangement can appear random and may change at any time.
237
+
238
+ This can be useful when you have objects with a finite set of keys and want a type defining only the allowed keys, but do not want to repeat yourself.
239
+
240
+ @example
241
+ ```
242
+ import type {UnionToTuple} from 'type-fest';
243
+
244
+ type Numbers = 1 | 2 | 3;
245
+ type NumbersTuple = UnionToTuple<Numbers>;
246
+ //=> [1, 2, 3]
247
+ ```
248
+
249
+ @example
250
+ ```
251
+ import type {UnionToTuple} from 'type-fest';
252
+
253
+ const pets = {
254
+ dog: '🐶',
255
+ cat: '🐱',
256
+ snake: '🐍',
257
+ };
258
+
259
+ type Pet = keyof typeof pets;
260
+ //=> 'dog' | 'cat' | 'snake'
261
+
262
+ const petList = Object.keys(pets) as UnionToTuple<Pet>;
263
+ //=> ['dog', 'cat', 'snake']
264
+ ```
265
+
266
+ @category Array
267
+ */
268
+ export type UnionToTuple<T, L = LastOfUnion<T>> = IsNever<T> extends false ? [
269
+ ...UnionToTuple<Exclude<T, L>>,
270
+ L
271
+ ] : [
272
+ ];
273
+ export type GetKey<Value> = {
274
+ [Key in keyof Values]: Value extends Values[Key] ? Key : never;
275
+ }[keyof Values];
276
+ export type GetTypes<Value extends unknown[]> = Value extends [
277
+ infer Single
278
+ ] ? Single : Value;
279
+ export type GetType<Value> = GetTypes<UnionToTuple<GetKey<Value>>>;
280
+ export type InferProperty<Value> = Value extends Property ? Value["type"] extends keyof Values ? Values[Value["type"]] : Value["type"] extends (keyof Values)[] ? Values[Value["type"][number]] : never : never;
281
+ export type InferValue<Value> = Value extends keyof Values ? Values[Value] : Value extends (keyof Values)[] ? Values[Value[number]] : never;
282
+ export type Inferred<Model extends Schema> = {
283
+ [Key in InferredRequiredProperties<Model>]: Model[Key] extends Property ? InferProperty<Model[Key]> : InferValue<Model[Key]>;
284
+ } & {
285
+ [Key in InferredOptionalProperties<Model>]?: Model[Key] extends Property ? InferProperty<Model[Key]> : never;
286
+ };
287
+ export type InferredOptionalProperties<Model extends Schema> = {
288
+ [Key in keyof Model]: Model[Key] extends Property ? Model[Key]["required"] extends false ? Key : never : never;
289
+ }[keyof Model];
290
+ export type InferredRequiredProperties<Model extends Schema> = {
291
+ [Key in keyof Model]: Model[Key] extends Property ? Model[Key]["required"] extends false ? never : Key : Key;
292
+ }[keyof Model];
293
+ export type OptionalProperty<Type> = {
294
+ required: false;
295
+ type: GetType<Type>;
296
+ };
297
+ export type Property = {
298
+ required?: boolean;
299
+ type: keyof Values | (keyof Values)[];
300
+ };
301
+ export type RequiredProperty<Type> = {
302
+ required: true;
303
+ type: GetType<Type>;
304
+ };
305
+ /**
306
+ * A schema for validating objects
307
+ */
308
+ export type Schema = Record<string, keyof Values | (keyof Values)[] | Property>;
309
+ /**
310
+ * A schematic for validating objects
311
+ */
312
+ export type Schematic<Model> = {
313
+ /**
314
+ * Does the value match the schema?
315
+ */
316
+ is(value: unknown): value is Model;
317
+ };
318
+ export type Typed = Record<string, unknown>;
319
+ /**
320
+ * A typed schema for validating objects
321
+ */
322
+ export type TypedSchema<Model extends Typed> = Simplify<{
323
+ [Key in RequiredKeysOf<Model>]: GetType<Model[Key]> | RequiredProperty<Model[Key]>;
324
+ } & {
325
+ [Key in OptionalKeysOf<Model>]: OptionalProperty<Model[Key]>;
326
+ }>;
327
+ export type Values = {
328
+ array: unknown[];
329
+ bigint: bigint;
330
+ boolean: boolean;
331
+ date: Date;
332
+ function: Function;
333
+ null: null;
334
+ number: number;
335
+ object: object;
336
+ string: string;
337
+ symbol: symbol;
338
+ undefined: undefined;
339
+ };
340
+ /**
341
+ * Create a schematic from a typed schema
342
+ */
343
+ export declare function schematic<Model extends Typed>(schema: TypedSchema<Model>): Schematic<Model>;
344
+ /**
345
+ * Create a schematic from a schema
346
+ */
347
+ export declare function schematic<Model extends Schema>(schema: Model): Schematic<Inferred<Model>>;
348
+
3
349
  export {};
package/types/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ export type { Schema, Schematic } from './model';
2
+ export * from './schematic';
@@ -0,0 +1,350 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ /**
4
+ Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
5
+
6
+ Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
7
+
8
+ @example
9
+ ```
10
+ import type {UnionToIntersection} from 'type-fest';
11
+
12
+ type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
13
+
14
+ type Intersection = UnionToIntersection<Union>;
15
+ //=> {the(): void; great(arg: string): void; escape: boolean};
16
+ ```
17
+
18
+ A more applicable example which could make its way into your library code follows.
19
+
20
+ @example
21
+ ```
22
+ import type {UnionToIntersection} from 'type-fest';
23
+
24
+ class CommandOne {
25
+ commands: {
26
+ a1: () => undefined,
27
+ b1: () => undefined,
28
+ }
29
+ }
30
+
31
+ class CommandTwo {
32
+ commands: {
33
+ a2: (argA: string) => undefined,
34
+ b2: (argB: string) => undefined,
35
+ }
36
+ }
37
+
38
+ const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
39
+ type Union = typeof union;
40
+ //=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
41
+
42
+ type Intersection = UnionToIntersection<Union>;
43
+ //=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
44
+ ```
45
+
46
+ @category Type
47
+ */
48
+ export type UnionToIntersection<Union> = (
49
+ // `extends unknown` is always going to be the case and is used to convert the
50
+ // `Union` into a [distributive conditional
51
+ // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
52
+ Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
53
+ /**
54
+ Extract all optional keys from the given type.
55
+
56
+ This is useful when you want to create a new type that contains different type values for the optional keys only.
57
+
58
+ @example
59
+ ```
60
+ import type {OptionalKeysOf, Except} from 'type-fest';
61
+
62
+ interface User {
63
+ name: string;
64
+ surname: string;
65
+
66
+ luckyNumber?: number;
67
+ }
68
+
69
+ const REMOVE_FIELD = Symbol('remove field symbol');
70
+ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
71
+ [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
72
+ };
73
+
74
+ const update1: UpdateOperation<User> = {
75
+ name: 'Alice'
76
+ };
77
+
78
+ const update2: UpdateOperation<User> = {
79
+ name: 'Bob',
80
+ luckyNumber: REMOVE_FIELD
81
+ };
82
+ ```
83
+
84
+ @category Utilities
85
+ */
86
+ export type OptionalKeysOf<BaseType extends object> = BaseType extends unknown // For distributing `BaseType`
87
+ ? (keyof {
88
+ [Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never;
89
+ }) & (keyof BaseType) // Intersect with `keyof BaseType` to ensure result of `OptionalKeysOf<BaseType>` is always assignable to `keyof BaseType`
90
+ : never; // Should never happen
91
+ /**
92
+ Extract all required keys from the given type.
93
+
94
+ This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
95
+
96
+ @example
97
+ ```
98
+ import type {RequiredKeysOf} from 'type-fest';
99
+
100
+ declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
101
+
102
+ interface User {
103
+ name: string;
104
+ surname: string;
105
+
106
+ luckyNumber?: number;
107
+ }
108
+
109
+ const validator1 = createValidation<User>('name', value => value.length < 25);
110
+ const validator2 = createValidation<User>('surname', value => value.length < 25);
111
+ ```
112
+
113
+ @category Utilities
114
+ */
115
+ export type RequiredKeysOf<BaseType extends object> = BaseType extends unknown // For distributing `BaseType`
116
+ ? Exclude<keyof BaseType, OptionalKeysOf<BaseType>> : never; // Should never happen
117
+ /**
118
+ Returns a boolean for whether the given type is `never`.
119
+
120
+ @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
121
+ @link https://stackoverflow.com/a/53984913/10292952
122
+ @link https://www.zhenghao.io/posts/ts-never
123
+
124
+ Useful in type utilities, such as checking if something does not occur.
125
+
126
+ @example
127
+ ```
128
+ import type {IsNever, And} from 'type-fest';
129
+
130
+ // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
131
+ type AreStringsEqual<A extends string, B extends string> =
132
+ And<
133
+ IsNever<Exclude<A, B>> extends true ? true : false,
134
+ IsNever<Exclude<B, A>> extends true ? true : false
135
+ >;
136
+
137
+ type EndIfEqual<I extends string, O extends string> =
138
+ AreStringsEqual<I, O> extends true
139
+ ? never
140
+ : void;
141
+
142
+ function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
143
+ if (input === output) {
144
+ process.exit(0);
145
+ }
146
+ }
147
+
148
+ endIfEqual('abc', 'abc');
149
+ //=> never
150
+
151
+ endIfEqual('abc', '123');
152
+ //=> void
153
+ ```
154
+
155
+ @category Type Guard
156
+ @category Utilities
157
+ */
158
+ export type IsNever<T> = [
159
+ T
160
+ ] extends [
161
+ never
162
+ ] ? true : false;
163
+ /**
164
+ Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
165
+
166
+ @example
167
+ ```
168
+ import type {Simplify} from 'type-fest';
169
+
170
+ type PositionProps = {
171
+ top: number;
172
+ left: number;
173
+ };
174
+
175
+ type SizeProps = {
176
+ width: number;
177
+ height: number;
178
+ };
179
+
180
+ // In your editor, hovering over `Props` will show a flattened object with all the properties.
181
+ type Props = Simplify<PositionProps & SizeProps>;
182
+ ```
183
+
184
+ Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
185
+
186
+ If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
187
+
188
+ @example
189
+ ```
190
+ import type {Simplify} from 'type-fest';
191
+
192
+ interface SomeInterface {
193
+ foo: number;
194
+ bar?: string;
195
+ baz: number | undefined;
196
+ }
197
+
198
+ type SomeType = {
199
+ foo: number;
200
+ bar?: string;
201
+ baz: number | undefined;
202
+ };
203
+
204
+ const literal = {foo: 123, bar: 'hello', baz: 456};
205
+ const someType: SomeType = literal;
206
+ const someInterface: SomeInterface = literal;
207
+
208
+ function fn(object: Record<string, unknown>): void {}
209
+
210
+ fn(literal); // Good: literal object type is sealed
211
+ fn(someType); // Good: type is sealed
212
+ fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
213
+ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
214
+ ```
215
+
216
+ @link https://github.com/microsoft/TypeScript/issues/15300
217
+ @see SimplifyDeep
218
+ @category Object
219
+ */
220
+ export type Simplify<T> = {
221
+ [KeyType in keyof T]: T[KeyType];
222
+ } & {};
223
+ /**
224
+ Returns the last element of a union type.
225
+
226
+ @example
227
+ ```
228
+ type Last = LastOfUnion<1 | 2 | 3>;
229
+ //=> 3
230
+ ```
231
+ */
232
+ export type LastOfUnion<T> = UnionToIntersection<T extends any ? () => T : never> extends () => (infer R) ? R : never;
233
+ /**
234
+ Convert a union type into an unordered tuple type of its elements.
235
+
236
+ "Unordered" means the elements of the tuple are not guaranteed to be in the same order as in the union type. The arrangement can appear random and may change at any time.
237
+
238
+ This can be useful when you have objects with a finite set of keys and want a type defining only the allowed keys, but do not want to repeat yourself.
239
+
240
+ @example
241
+ ```
242
+ import type {UnionToTuple} from 'type-fest';
243
+
244
+ type Numbers = 1 | 2 | 3;
245
+ type NumbersTuple = UnionToTuple<Numbers>;
246
+ //=> [1, 2, 3]
247
+ ```
248
+
249
+ @example
250
+ ```
251
+ import type {UnionToTuple} from 'type-fest';
252
+
253
+ const pets = {
254
+ dog: '🐶',
255
+ cat: '🐱',
256
+ snake: '🐍',
257
+ };
258
+
259
+ type Pet = keyof typeof pets;
260
+ //=> 'dog' | 'cat' | 'snake'
261
+
262
+ const petList = Object.keys(pets) as UnionToTuple<Pet>;
263
+ //=> ['dog', 'cat', 'snake']
264
+ ```
265
+
266
+ @category Array
267
+ */
268
+ export type UnionToTuple<T, L = LastOfUnion<T>> = IsNever<T> extends false ? [
269
+ ...UnionToTuple<Exclude<T, L>>,
270
+ L
271
+ ] : [
272
+ ];
273
+ export type GetKey<Value> = {
274
+ [Key in keyof Values]: Value extends Values[Key] ? Key : never;
275
+ }[keyof Values];
276
+ export type GetTypes<Value extends unknown[]> = Value extends [
277
+ infer Single
278
+ ] ? Single : Value;
279
+ export type GetType<Value> = GetTypes<UnionToTuple<GetKey<Value>>>;
280
+ export type InferProperty<Value> = Value extends Property ? Value["type"] extends keyof Values ? Values[Value["type"]] : Value["type"] extends (keyof Values)[] ? Values[Value["type"][number]] : never : never;
281
+ export type InferValue<Value> = Value extends keyof Values ? Values[Value] : Value extends (keyof Values)[] ? Values[Value[number]] : never;
282
+ export type Inferred<Model extends Schema> = {
283
+ [Key in InferredRequiredProperties<Model>]: Model[Key] extends Property ? InferProperty<Model[Key]> : InferValue<Model[Key]>;
284
+ } & {
285
+ [Key in InferredOptionalProperties<Model>]?: Model[Key] extends Property ? InferProperty<Model[Key]> : never;
286
+ };
287
+ export type InferredOptionalProperties<Model extends Schema> = {
288
+ [Key in keyof Model]: Model[Key] extends Property ? Model[Key]["required"] extends false ? Key : never : never;
289
+ }[keyof Model];
290
+ export type InferredRequiredProperties<Model extends Schema> = {
291
+ [Key in keyof Model]: Model[Key] extends Property ? Model[Key]["required"] extends false ? never : Key : Key;
292
+ }[keyof Model];
293
+ export type OptionalProperty<Type> = {
294
+ required: false;
295
+ type: GetType<Type>;
296
+ };
297
+ export type Property = {
298
+ required?: boolean;
299
+ type: keyof Values | (keyof Values)[];
300
+ };
301
+ export type RequiredProperty<Type> = {
302
+ required: true;
303
+ type: GetType<Type>;
304
+ };
305
+ /**
306
+ * A schema for validating objects
307
+ */
308
+ export type Schema = Record<string, keyof Values | (keyof Values)[] | Property>;
309
+ /**
310
+ * A schematic for validating objects
311
+ */
312
+ export type Schematic<Model> = {
313
+ /**
314
+ * Does the value match the schema?
315
+ */
316
+ is(value: unknown): value is Model;
317
+ };
318
+ export type Typed = Record<string, unknown>;
319
+ /**
320
+ * A typed schema for validating objects
321
+ */
322
+ export type TypedSchema<Model extends Typed> = Simplify<{
323
+ [Key in RequiredKeysOf<Model>]: GetType<Model[Key]> | RequiredProperty<Model[Key]>;
324
+ } & {
325
+ [Key in OptionalKeysOf<Model>]: OptionalProperty<Model[Key]>;
326
+ }>;
327
+ export type ValidatedProperty = {
328
+ required: boolean;
329
+ types: (keyof Values)[];
330
+ };
331
+ export type ValidatedSchema = {
332
+ keys: string[];
333
+ length: number;
334
+ properties: Record<string, ValidatedProperty>;
335
+ };
336
+ export type Values = {
337
+ array: unknown[];
338
+ bigint: bigint;
339
+ boolean: boolean;
340
+ date: Date;
341
+ function: Function;
342
+ null: null;
343
+ number: number;
344
+ object: object;
345
+ string: string;
346
+ symbol: symbol;
347
+ undefined: undefined;
348
+ };
349
+
350
+ export {};