typebad 1.0.3 → 1.1.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/LICENSE +1 -1
- package/dist/cjs/Type.d.ts +31 -19
- package/dist/esm/Type.d.ts +31 -19
- package/package.json +1 -1
package/LICENSE
CHANGED
package/dist/cjs/Type.d.ts
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
|
-
export type
|
|
2
|
-
export type
|
|
3
|
-
[K in keyof T
|
|
4
|
-
} & {
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export type TypeInternal<T> = T extends Type<infer U> ? U : never;
|
|
2
|
+
export type TypeInternalSchema<T extends TypeSchema> = {
|
|
3
|
+
[K in keyof T]: TypeInternal<T[K]>;
|
|
4
|
+
} & {};
|
|
5
|
+
export type TypeSchema = Record<string, Type<any>>;
|
|
6
|
+
export type ConstType<T> = T & {
|
|
7
|
+
__const: true;
|
|
8
|
+
};
|
|
7
9
|
type Simplify<T> = {
|
|
8
10
|
[K in keyof T]: T[K];
|
|
9
11
|
} & {};
|
|
10
|
-
export type TypeSchema = Record<string, Type<any>>;
|
|
11
12
|
type IsTuple<T> = T extends readonly any[] ? number extends T["length"] ? false : true : false;
|
|
12
|
-
export type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
export type TypeInput<T> = T extends Type<infer U> ? TypeInputValue<U> : never;
|
|
14
|
+
export type TypeInputValue<T> = T extends ConstType<infer U> ? U : IsTuple<T> extends true ? {
|
|
15
|
+
[K in keyof T]: TypeInputValue<T[K]>;
|
|
16
|
+
} : T extends (infer U)[] ? TypeInputValue<U>[] : T extends object ? TypeInputObject<T> : T;
|
|
17
|
+
export type TypeInputObject<T> = Simplify<{
|
|
18
|
+
[K in keyof T as undefined extends TypeInputValue<T[K]> ? never : unknown extends TypeInputValue<T[K]> ? never : K]: TypeInputValue<T[K]>;
|
|
19
|
+
} & {
|
|
20
|
+
[K in keyof T as undefined extends TypeInputValue<T[K]> ? unknown extends TypeInputValue<T[K]> ? never : K : never]?: TypeInputValue<T[K]>;
|
|
21
|
+
}>;
|
|
22
|
+
export type TypeOutput<T extends Type<any>> = TypeOutputValue<TypeInternal<T>>;
|
|
23
|
+
export type TypeOutputValue<T> = T extends undefined ? TypeOutputValue<Exclude<T, undefined>> : T extends ConstType<infer U> ? U : IsTuple<T> extends true ? {
|
|
24
|
+
[K in keyof T]: TypeOutputValue<T[K]>;
|
|
25
|
+
} : T extends (infer U)[] ? TypeOutputValue<U>[] : T extends object ? {
|
|
26
|
+
[K in keyof T]: TypeOutputValue<T[K]>;
|
|
16
27
|
} : T;
|
|
17
|
-
type ExtendedValue<T> = T extends
|
|
18
|
-
|
|
28
|
+
type ExtendedValue<T> = T extends ConstType<infer U> ? U : IsTuple<T> extends true ? {
|
|
29
|
+
[K in keyof T]: any;
|
|
30
|
+
} : T extends any[] ? any[] : T extends object ? Record<string, any> : T;
|
|
19
31
|
export interface ParseOptions {
|
|
20
32
|
allowUnknownProperties: boolean;
|
|
21
33
|
}
|
|
@@ -31,14 +43,14 @@ export declare class Type<T> {
|
|
|
31
43
|
static match<T>(matcher: (value: any) => boolean): Type<T>;
|
|
32
44
|
static enum<const V extends readonly (string | number | boolean)[]>(values: V): Type<V[number]>;
|
|
33
45
|
static value<const V extends string | number | boolean>(value: V): Type<V | undefined>;
|
|
34
|
-
static array<T extends Type<any>>(elementType: T): Type<
|
|
35
|
-
static object<S extends TypeSchema>(schema: S): Type<
|
|
36
|
-
static union<U extends Type<any>[]>(types: U): Type<
|
|
46
|
+
static array<T extends Type<any>>(elementType: T): Type<TypeInternal<T>[]>;
|
|
47
|
+
static object<S extends TypeSchema>(schema: S): Type<TypeInternalSchema<S>>;
|
|
48
|
+
static union<U extends Type<any>[]>(types: U): Type<TypeInternal<U[number]>>;
|
|
37
49
|
static tuple<const U extends Type<any>[]>(types: U): Type<{
|
|
38
|
-
[K in keyof U]:
|
|
50
|
+
[K in keyof U]: TypeInternal<U[K]>;
|
|
39
51
|
}>;
|
|
40
52
|
static lazy<T extends Type<any>>(callback: () => T): T;
|
|
41
|
-
static parse<T extends Type<any>>(type: T, value:
|
|
53
|
+
static parse<T extends Type<any>>(type: T, value: TypeInput<T>, options?: Partial<ParseOptions>): TypeOutput<T>;
|
|
42
54
|
private static getValueError;
|
|
43
55
|
private static getUnknownPropertyError;
|
|
44
56
|
private static getMissingPropertyError;
|
|
@@ -47,6 +59,6 @@ export declare class Type<T> {
|
|
|
47
59
|
static readonly Number: Type<number>;
|
|
48
60
|
static readonly Integer: Type<number>;
|
|
49
61
|
static readonly Float: Type<number>;
|
|
50
|
-
static readonly Date: Type<Date
|
|
62
|
+
static readonly Date: Type<ConstType<Date>>;
|
|
51
63
|
}
|
|
52
64
|
export {};
|
package/dist/esm/Type.d.ts
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
|
-
export type
|
|
2
|
-
export type
|
|
3
|
-
[K in keyof T
|
|
4
|
-
} & {
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export type TypeInternal<T> = T extends Type<infer U> ? U : never;
|
|
2
|
+
export type TypeInternalSchema<T extends TypeSchema> = {
|
|
3
|
+
[K in keyof T]: TypeInternal<T[K]>;
|
|
4
|
+
} & {};
|
|
5
|
+
export type TypeSchema = Record<string, Type<any>>;
|
|
6
|
+
export type ConstType<T> = T & {
|
|
7
|
+
__const: true;
|
|
8
|
+
};
|
|
7
9
|
type Simplify<T> = {
|
|
8
10
|
[K in keyof T]: T[K];
|
|
9
11
|
} & {};
|
|
10
|
-
export type TypeSchema = Record<string, Type<any>>;
|
|
11
12
|
type IsTuple<T> = T extends readonly any[] ? number extends T["length"] ? false : true : false;
|
|
12
|
-
export type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
export type TypeInput<T> = T extends Type<infer U> ? TypeInputValue<U> : never;
|
|
14
|
+
export type TypeInputValue<T> = T extends ConstType<infer U> ? U : IsTuple<T> extends true ? {
|
|
15
|
+
[K in keyof T]: TypeInputValue<T[K]>;
|
|
16
|
+
} : T extends (infer U)[] ? TypeInputValue<U>[] : T extends object ? TypeInputObject<T> : T;
|
|
17
|
+
export type TypeInputObject<T> = Simplify<{
|
|
18
|
+
[K in keyof T as undefined extends TypeInputValue<T[K]> ? never : unknown extends TypeInputValue<T[K]> ? never : K]: TypeInputValue<T[K]>;
|
|
19
|
+
} & {
|
|
20
|
+
[K in keyof T as undefined extends TypeInputValue<T[K]> ? unknown extends TypeInputValue<T[K]> ? never : K : never]?: TypeInputValue<T[K]>;
|
|
21
|
+
}>;
|
|
22
|
+
export type TypeOutput<T extends Type<any>> = TypeOutputValue<TypeInternal<T>>;
|
|
23
|
+
export type TypeOutputValue<T> = T extends undefined ? TypeOutputValue<Exclude<T, undefined>> : T extends ConstType<infer U> ? U : IsTuple<T> extends true ? {
|
|
24
|
+
[K in keyof T]: TypeOutputValue<T[K]>;
|
|
25
|
+
} : T extends (infer U)[] ? TypeOutputValue<U>[] : T extends object ? {
|
|
26
|
+
[K in keyof T]: TypeOutputValue<T[K]>;
|
|
16
27
|
} : T;
|
|
17
|
-
type ExtendedValue<T> = T extends
|
|
18
|
-
|
|
28
|
+
type ExtendedValue<T> = T extends ConstType<infer U> ? U : IsTuple<T> extends true ? {
|
|
29
|
+
[K in keyof T]: any;
|
|
30
|
+
} : T extends any[] ? any[] : T extends object ? Record<string, any> : T;
|
|
19
31
|
export interface ParseOptions {
|
|
20
32
|
allowUnknownProperties: boolean;
|
|
21
33
|
}
|
|
@@ -31,14 +43,14 @@ export declare class Type<T> {
|
|
|
31
43
|
static match<T>(matcher: (value: any) => boolean): Type<T>;
|
|
32
44
|
static enum<const V extends readonly (string | number | boolean)[]>(values: V): Type<V[number]>;
|
|
33
45
|
static value<const V extends string | number | boolean>(value: V): Type<V | undefined>;
|
|
34
|
-
static array<T extends Type<any>>(elementType: T): Type<
|
|
35
|
-
static object<S extends TypeSchema>(schema: S): Type<
|
|
36
|
-
static union<U extends Type<any>[]>(types: U): Type<
|
|
46
|
+
static array<T extends Type<any>>(elementType: T): Type<TypeInternal<T>[]>;
|
|
47
|
+
static object<S extends TypeSchema>(schema: S): Type<TypeInternalSchema<S>>;
|
|
48
|
+
static union<U extends Type<any>[]>(types: U): Type<TypeInternal<U[number]>>;
|
|
37
49
|
static tuple<const U extends Type<any>[]>(types: U): Type<{
|
|
38
|
-
[K in keyof U]:
|
|
50
|
+
[K in keyof U]: TypeInternal<U[K]>;
|
|
39
51
|
}>;
|
|
40
52
|
static lazy<T extends Type<any>>(callback: () => T): T;
|
|
41
|
-
static parse<T extends Type<any>>(type: T, value:
|
|
53
|
+
static parse<T extends Type<any>>(type: T, value: TypeInput<T>, options?: Partial<ParseOptions>): TypeOutput<T>;
|
|
42
54
|
private static getValueError;
|
|
43
55
|
private static getUnknownPropertyError;
|
|
44
56
|
private static getMissingPropertyError;
|
|
@@ -47,6 +59,6 @@ export declare class Type<T> {
|
|
|
47
59
|
static readonly Number: Type<number>;
|
|
48
60
|
static readonly Integer: Type<number>;
|
|
49
61
|
static readonly Float: Type<number>;
|
|
50
|
-
static readonly Date: Type<Date
|
|
62
|
+
static readonly Date: Type<ConstType<Date>>;
|
|
51
63
|
}
|
|
52
64
|
export {};
|