@bemedev/typings 1.1.3 → 1.1.5
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/README.md +46 -0
- package/lib/type.cjs +4 -1
- package/lib/type.d.ts +1 -0
- package/lib/type.js +4 -1
- package/lib/types.d.ts +11 -7
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -40,6 +40,52 @@ expectTypeOf(result).toEqualTypeOf<{
|
|
|
40
40
|
|
|
41
41
|
<br/>
|
|
42
42
|
|
|
43
|
+
## Pre-processing Types with `pretype`
|
|
44
|
+
|
|
45
|
+
The `pretype` function allows you to pre-process type definitions before
|
|
46
|
+
transformation, enabling type context chaining and reusable type pipelines:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { type, pretype } from '@bemedev/typings';
|
|
50
|
+
|
|
51
|
+
const baseType = type(({ partial, record, primitiveObject }) =>
|
|
52
|
+
partial({
|
|
53
|
+
emitters: record({
|
|
54
|
+
next: primitiveObject.const,
|
|
55
|
+
error: primitiveObject.const,
|
|
56
|
+
}),
|
|
57
|
+
children: record(primitiveObject.map.const),
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const pretypedResult = pretype(baseType);
|
|
62
|
+
|
|
63
|
+
// Access the pre-processed type context
|
|
64
|
+
const finalType = pretypedResult({
|
|
65
|
+
emitters: {
|
|
66
|
+
data: { next: 'string', error: 'string' },
|
|
67
|
+
},
|
|
68
|
+
children: { eventMap: 'string' },
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
expectTypeOf(finalType.type).toEqualTypeOf<{
|
|
72
|
+
emitters?: { data: { next: string; error: string } };
|
|
73
|
+
children?: { eventMap: string };
|
|
74
|
+
}>();
|
|
75
|
+
|
|
76
|
+
// The pretype result includes the original type definition
|
|
77
|
+
expectTypeOf(pretypedResult.pretype).toEqualTypeOf(baseType);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
N.B. The `pretype` function is designed to work with the type definitions
|
|
81
|
+
created using the `type` function, allowing you to create complex type
|
|
82
|
+
transformations while maintaining access to the original type context for
|
|
83
|
+
further processing or reuse. Also `pretype.pretype` is a getter to the
|
|
84
|
+
original type definition, which can be useful for chaining or referencing
|
|
85
|
+
the base type in multiple transformations.
|
|
86
|
+
|
|
87
|
+
<br/>
|
|
88
|
+
|
|
43
89
|
## Available Helpers
|
|
44
90
|
|
|
45
91
|
- `any`: Any type
|
package/lib/type.cjs
CHANGED
|
@@ -52,7 +52,10 @@ const type = (option) => {
|
|
|
52
52
|
else out = _transform(option);
|
|
53
53
|
return require_standard.standardize(out);
|
|
54
54
|
};
|
|
55
|
-
const pretype = (
|
|
55
|
+
const pretype = (pretype) => require_utils_expandFn.expandFn(type, {
|
|
56
|
+
type,
|
|
57
|
+
pretype: require_standard.standardize(pretype?.__type ?? "any")
|
|
58
|
+
});
|
|
56
59
|
//#endregion
|
|
57
60
|
exports.pretype = pretype;
|
|
58
61
|
exports.type = type;
|
package/lib/type.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export type Transform_F = <T extends ObjectT = ObjectT>(option?: ((helpers: Help
|
|
|
25
25
|
type _PreTransform_F<U extends ObjectT> = <T extends SafePre<U> = SafePre<U>>(option?: ((helpers: Helpers) => T) | T) => inferSh<T>;
|
|
26
26
|
export type PreTransform_F = <U extends ObjectT>(_?: inferSh<U>) => FnBasic<_PreTransform_F<U>, {
|
|
27
27
|
type: _PreTransform_F<U>;
|
|
28
|
+
pretype: inferSh<U>;
|
|
28
29
|
}>;
|
|
29
30
|
export declare const type: Transform_F;
|
|
30
31
|
export declare const pretype: PreTransform_F;
|
package/lib/type.js
CHANGED
|
@@ -51,6 +51,9 @@ const type = (option) => {
|
|
|
51
51
|
else out = _transform(option);
|
|
52
52
|
return standardize(out);
|
|
53
53
|
};
|
|
54
|
-
const pretype = (
|
|
54
|
+
const pretype = (pretype) => expandFn(type, {
|
|
55
|
+
type,
|
|
56
|
+
pretype: standardize(pretype?.__type ?? "any")
|
|
57
|
+
});
|
|
55
58
|
//#endregion
|
|
56
59
|
export { pretype, type };
|
package/lib/types.d.ts
CHANGED
|
@@ -66,7 +66,7 @@ export type IntersectionCustom<T extends ObjectMapS[]> = T extends [
|
|
|
66
66
|
...infer Rest extends ObjectMapS[]
|
|
67
67
|
] ? First & IntersectionCustom<Rest> : unknown;
|
|
68
68
|
type _ObjectT = __ObjectT | Optional | ArrayCustom;
|
|
69
|
-
export type PrimitiveObjectT = SoRa<
|
|
69
|
+
export type PrimitiveObjectT = SoRa<PrimitiveT | PrimitiveObjectMapS | ArrayCustom<PrimitiveT | PrimitiveObjectMapS> | Optional<PrimitiveT | PrimitiveObjectMapS> | UnionCustom<(PrimitiveObjectMapS | PrimitiveT)[]> | PartialCustom<PrimitiveObjectMapS>>;
|
|
70
70
|
export interface PrimitiveObjectMapS {
|
|
71
71
|
[key: Keys]: PrimitiveObjectT;
|
|
72
72
|
}
|
|
@@ -87,16 +87,16 @@ type ReduceTupleU<T extends AnyArray> = T extends [
|
|
|
87
87
|
] ? [Undefiny<First>, ...ReduceTupleU<Rest>] : T[number] extends never ? [] : T['length'] extends 0 ? [] : number extends T['length'] ? T : Undefiny<T[number]>[];
|
|
88
88
|
type EmptyObject = {};
|
|
89
89
|
type __TransformUnion<T extends UnionCustom> = T extends UnionCustom<infer TCustom> ? Omit<T, typeof UNION> extends infer Ot ? Equals<Ot, EmptyObject> extends true ? TransformT<TCustom[number]> : TransformT<TCustom[number]> & TransformT<Ot> : never : never;
|
|
90
|
-
type HasUndefined<T> = unknown extends T ? false : OptionalHelperClass extends T ? true : false;
|
|
90
|
+
type HasUndefined<T> = unknown extends T ? false : EmptyObject extends T ? false : OptionalHelperClass extends T ? true : false;
|
|
91
91
|
type UndefinyObject<T extends object> = {
|
|
92
92
|
[K in keyof T as HasUndefined<T[K]> extends true ? never : K]: Undefiny<T[K]>;
|
|
93
93
|
} & {
|
|
94
94
|
[K in keyof T as HasUndefined<T[K]> extends true ? K : never]?: Undefiny<T[K]>;
|
|
95
|
-
} extends infer
|
|
96
|
-
[K in keyof
|
|
95
|
+
} extends infer O ? {
|
|
96
|
+
[K in keyof O]: O[K];
|
|
97
97
|
} : never;
|
|
98
|
-
type Undefiny<T, U = Exclude<T, OptionalHelperClass>> = U extends AnyArray ? ReduceTupleU<U> : U extends
|
|
99
|
-
type TransformT<T> = Equals<ObjectMapS, T> extends true ? object : T extends Types ? TransformTypes<T> : T extends ArrayCustom<infer A> ? TransformT<A>[] : T extends UnionCustom ? __TransformUnion<T> : T extends SoRaCustom<infer TSoA> ? SoRa<TransformT<TSoA>> : T extends SoaCustom<infer TSoA> ? SoA<TransformT<TSoA>> : T extends Custom<infer TCustom> ? TCustom : T extends AnyArray<ObjectT> ? ReduceTuple2<T> : T extends PartialCustom<infer TPartial> ? Partial<TransformT<TPartial>> : T extends Optional<infer TOptional> ? TransformT<TOptional> | OptionalHelperClass : Undefiny<{
|
|
98
|
+
type Undefiny<T, U = Exclude<T, OptionalHelperClass>> = U extends AnyArray ? ReduceTupleU<U> : U extends TrueObject ? UndefinyObject<U> : U;
|
|
99
|
+
type TransformT<T> = EmptyObject extends T ? EmptyObject : Equals<ObjectMapS, T> extends true ? object : T extends Types ? TransformTypes<T> : T extends ArrayCustom<infer A> ? TransformT<A>[] : T extends UnionCustom ? __TransformUnion<T> : T extends SoRaCustom<infer TSoA> ? SoRa<TransformT<TSoA>> : T extends SoaCustom<infer TSoA> ? SoA<TransformT<TSoA>> : T extends Custom<infer TCustom> ? TCustom : T extends AnyArray<ObjectT> ? ReduceTuple2<T> : T extends PartialCustom<infer TPartial> ? Partial<TransformT<TPartial>> : T extends Optional<infer TOptional> ? TransformT<TOptional> | OptionalHelperClass : Undefiny<{
|
|
100
100
|
[K in keyof T]: TransformT<T[K]>;
|
|
101
101
|
}>;
|
|
102
102
|
export type StandardHelper<T1 = any, T2 = any> = {
|
|
@@ -112,7 +112,11 @@ export type StandardOutput<T = any> = {
|
|
|
112
112
|
};
|
|
113
113
|
};
|
|
114
114
|
};
|
|
115
|
-
export type
|
|
115
|
+
export type PrimitiveObject = SoRa<Primitive | PrimitiveObjectMap>;
|
|
116
|
+
export interface PrimitiveObjectMap {
|
|
117
|
+
[key: Keys]: PrimitiveObject;
|
|
118
|
+
}
|
|
119
|
+
export type inferO<T extends ObjectT = ObjectT> = EmptyObject extends T ? EmptyObject : PrimitiveObjectT extends T ? PrimitiveObject : T extends Optional<infer U> ? TransformT<U> | undefined : TransformT<T>;
|
|
116
120
|
export type inferSh<T extends ObjectT = ObjectT> = _Sh<T, inferO<T>>;
|
|
117
121
|
export type inferT<T extends StandardOutput = StandardOutput> = Exclude<T[typeof STANDARD_KEY]['types'], undefined>['output'];
|
|
118
122
|
export type ProduceObject<T extends ObjectT = ObjectT> = T;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemedev/typings",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "Typings by variables",
|
|
5
5
|
"author": {
|
|
6
6
|
"email": "bri_lvi@icloud.com",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
},
|
|
70
70
|
"scripts": {
|
|
71
71
|
"build": "pnpm rm:lib && pnpm run rolldown",
|
|
72
|
-
"_ci": "pnpm run config:off && pnpm run lint && pnpm run test",
|
|
72
|
+
"_ci": "pnpm clean --lockfile && pnpm run config:off && pnpm run lint && pnpm run test",
|
|
73
73
|
"ci": "CI_START=$(date +%s) && pnpm run _ci && pnpm run p-q && echo \"\n✅ CI takes $(($(date +%s) - CI_START))s\n\"",
|
|
74
74
|
"ci:admin": "CI_START=$(date +%s) && pnpm run rm && pnpm run upgrade:fast && pnpm run _ci && echo \"\n✅ Admin CI takes $(($(date +%s) - CI_START))s\n\"",
|
|
75
75
|
"clean": "pnpm run rm && pnpm run config",
|