@bemedev/typings
v1.5.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.
Custom Types inside Primitive Objects
The primitiveObject helper accepts a restricted Custom type. Specifically, it
allows custom validations for types extending string, number, or boolean (i.e.,
Exclude<JSON_Primitive, undefined>).
import { type } from '@bemedev/typings';
const result = type(({ primitiveObject, custom }) =>
primitiveObject({
id: custom<string>(), // Custom validator for string
age: custom<number>(), // Custom validator for number
}),
);Available Helpers
any: Any typearray: Array of typescustom: Custom typefn: Function schema helper (aliasfunction)intersection: 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 typesuse: Extracts underlying__typeschema definition from a Standard Schema object
Exports
The package supports the following subpath exports for both ESM and CommonJS:
@bemedev/typings: Main entry point (type,pretype, etc.)@bemedev/typings/helpers: Schema helper functions (array,object,use, etc.)@bemedev/typings/constants: Internal constants and type markers@bemedev/typings/types: TypeScript types and interfaces@bemedev/typings/standard: Standard Schema utilities (standardize,isSh, etc.)@bemedev/typings/utils/*: Utility functions (e.g.isSh,expandFn)
License
MIT
CHANGELOG
Author
chlbri ([email protected])
