@bemedev/typings 1.1.2 → 1.1.4
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 +5 -1
- package/lib/type.d.ts +8 -7
- package/lib/type.js +5 -1
- package/lib/types.d.ts +8 -0
- package/package.json +1 -1
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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_standard = require("./standard.cjs");
|
|
3
|
+
const require_utils_expandFn = require("./utils/expandFn.cjs");
|
|
3
4
|
const require_helpers_any = require("./helpers/any.cjs");
|
|
4
5
|
const require_helpers_array = require("./helpers/array.cjs");
|
|
5
6
|
const require_helpers_custom = require("./helpers/custom.cjs");
|
|
@@ -51,7 +52,10 @@ const type = (option) => {
|
|
|
51
52
|
else out = _transform(option);
|
|
52
53
|
return require_standard.standardize(out);
|
|
53
54
|
};
|
|
54
|
-
const pretype = (
|
|
55
|
+
const pretype = (pretype) => require_utils_expandFn.expandFn(type, {
|
|
56
|
+
type,
|
|
57
|
+
pretype: require_standard.standardize(pretype?.__type ?? "any")
|
|
58
|
+
});
|
|
55
59
|
//#endregion
|
|
56
60
|
exports.pretype = pretype;
|
|
57
61
|
exports.type = type;
|
package/lib/type.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { inferSh, ObjectT } from './types';
|
|
2
|
-
import { any, array, custom, intersection, litterals, object, omit, optional, partial, primitive, primitiveObject, readonly, record, soa, sv, tuple, union,
|
|
1
|
+
import type { FnBasic, inferSh, ObjectT, SafePre } from './types';
|
|
2
|
+
import { any, array, custom, intersection, litterals, object, omit, optional, partial, primitive, primitiveObject, readonly, record, soa, sora, sv, tuple, union, use } from './helpers';
|
|
3
3
|
type Helpers = {
|
|
4
4
|
any: typeof any;
|
|
5
5
|
custom: typeof custom;
|
|
@@ -22,10 +22,11 @@ type Helpers = {
|
|
|
22
22
|
sora: typeof sora;
|
|
23
23
|
};
|
|
24
24
|
export type Transform_F = <T extends ObjectT = ObjectT>(option?: ((helpers: Helpers) => T) | T) => inferSh<T>;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type: <
|
|
28
|
-
|
|
25
|
+
type _PreTransform_F<U extends ObjectT> = <T extends SafePre<U> = SafePre<U>>(option?: ((helpers: Helpers) => T) | T) => inferSh<T>;
|
|
26
|
+
export type PreTransform_F = <U extends ObjectT>(_?: inferSh<U>) => FnBasic<_PreTransform_F<U>, {
|
|
27
|
+
type: _PreTransform_F<U>;
|
|
28
|
+
pretype: inferSh<U>;
|
|
29
|
+
}>;
|
|
29
30
|
export declare const type: Transform_F;
|
|
30
|
-
export declare const pretype:
|
|
31
|
+
export declare const pretype: PreTransform_F;
|
|
31
32
|
export {};
|
package/lib/type.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { standardize } from "./standard.js";
|
|
2
|
+
import { expandFn } from "./utils/expandFn.js";
|
|
2
3
|
import { any } from "./helpers/any.js";
|
|
3
4
|
import { array } from "./helpers/array.js";
|
|
4
5
|
import { custom } from "./helpers/custom.js";
|
|
@@ -50,6 +51,9 @@ const type = (option) => {
|
|
|
50
51
|
else out = _transform(option);
|
|
51
52
|
return standardize(out);
|
|
52
53
|
};
|
|
53
|
-
const pretype = (
|
|
54
|
+
const pretype = (pretype) => expandFn(type, {
|
|
55
|
+
type,
|
|
56
|
+
pretype: standardize(pretype?.__type ?? "any")
|
|
57
|
+
});
|
|
54
58
|
//#endregion
|
|
55
59
|
export { pretype, type };
|
package/lib/types.d.ts
CHANGED
|
@@ -117,4 +117,12 @@ export type inferSh<T extends ObjectT = ObjectT> = _Sh<T, inferO<T>>;
|
|
|
117
117
|
export type inferT<T extends StandardOutput = StandardOutput> = Exclude<T[typeof STANDARD_KEY]['types'], undefined>['output'];
|
|
118
118
|
export type ProduceObject<T extends ObjectT = ObjectT> = T;
|
|
119
119
|
export type FnBasic<Main extends Fn, Tr extends object> = Tr & Main;
|
|
120
|
+
type ReduceTupleSafePre<T extends ReadonlyArray<ObjectT>> = T extends readonly [
|
|
121
|
+
infer First extends ObjectT,
|
|
122
|
+
...infer Rest extends ReadonlyArray<ObjectT>
|
|
123
|
+
] ? readonly [SafePre<First>, ...ReduceTupleSafePre<Rest>] : [];
|
|
124
|
+
type _SafePre<T extends ObjectT> = PrimitiveObjectT extends T ? PrimitiveObjectT : PrimitiveT extends T ? PrimitiveT : T extends Types ? T : T extends PartialCustom<infer TPartial> ? Partial<SafePre<TPartial>> : T extends ArrayCustom<infer A> ? SafePre<A>[] : T extends Optional<infer TOptional> ? SafePre<TOptional> | undefined : T extends SoRaCustom<infer TSoRa> ? SoRa<SafePre<TSoRa>> : T extends SoaCustom<infer TSoA> ? SoA<SafePre<TSoA>> : T extends Custom ? T : T extends AnyArray<ObjectT> ? ReduceTupleSafePre<T> : PrimitiveObjectMapS extends T ? PrimitiveObjectMapS : ObjectMapS extends T ? ObjectMapS : T extends ObjectMapS ? {
|
|
125
|
+
[K in keyof T]: SafePre<T[K]>;
|
|
126
|
+
} : T;
|
|
127
|
+
export type SafePre<T extends ObjectT> = Extract<_SafePre<T>, ObjectT>;
|
|
120
128
|
export * from './standard.types';
|