@bemedev/typings
v1.2.0
Published
Typings by variables
Readme
@bemedev/typings
Typings by variables
Usage
import { type } from '@bemedev/typings';
const result = type(({ array, optional, intersection }) => ({
nodes: optional(
array(
intersection(
{
position: {
x: 'number',
y: 'number',
},
data: {
label: optional('string'),
content: 'string',
},
input: 'boolean',
},
{ id: 'string' },
),
),
),
}));
expectTypeOf(result).toEqualTypeOf<{
nodes?: Array<{
position: { x: number; y: number };
data: { label?: string; content: string };
input: boolean;
id: string;
}>;
}>();Pre-processing Types with pretype
The pretype function allows you to pre-process type definitions before
transformation, enabling type context chaining and reusable type pipelines:
import { type, pretype } from '@bemedev/typings';
const baseType = type(({ partial, record, primitiveObject }) =>
partial({
emitters: record({
next: primitiveObject.const,
error: primitiveObject.const,
}),
children: record(primitiveObject.map.const),
}),
);
const pretypedResult = pretype(baseType);
// Access the pre-processed type context
const finalType = pretypedResult({
emitters: {
data: { next: 'string', error: 'string' },
},
children: { eventMap: 'string' },
});
expectTypeOf(finalType.type).toEqualTypeOf<{
emitters?: { data: { next: string; error: string } };
children?: { eventMap: string };
}>();
// The pretype result includes the original type definition
expectTypeOf(pretypedResult.pretype).toEqualTypeOf(baseType);N.B. The pretype function is designed to work with the type definitions
created using the type function, allowing you to create complex type
transformations while maintaining access to the original type context for
further processing or reuse. Also pretype.pretype is a getter to the
original type definition, which can be useful for chaining or referencing
the base type in multiple transformations.
Available Helpers
any: Any typearray: Array of typescustom: Custom typeintersection: Intersection of typeslitterals: Literal typesobject: Object schema helperomit: Omit specific keys from an objectoptional: Optional typespartial: Partial typesprimitive: Primitive type definitionsprimitiveObject: Primitive object schemareadonly: Readonly object typesrecord: Record typessoa: Single or Array typessora: Single or Array types, with recursive support, (soa with recursion)sv: State Valuetuple: Tuple typesunion: Union types
Licence
MIT
CHANGELOG
Auteur
chlbri ([email protected])
