@bemedev/typings 1.1.3 → 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 +4 -1
- package/lib/type.d.ts +1 -0
- package/lib/type.js +4 -1
- 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
|
@@ -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 };
|