@aliaksandarpratashchyk/ie
v1.0.0
Published
Runtime type definitions with conversion, decorators, and snapshots for TypeScript.
Maintainers
Readme
I.E. (Id Est)
Runtime type definitions for TypeScript with validation, target-based conversions, and decorator-powered metadata.
Features
- Runtime types for primitives, objects, tuples, unions, literals, enumerations, arrays, binary data, dates, and constructable classes.
- Validate unknown values at runtime via
type.is(value). - Convert values to/from well-known targets (for example
jsonandshallowSnapshot) viatype.to.<target>/type.from.<target>. - Decorate class properties/parameters with runtime types and build a
constructabletype from metadata.
Installation
npm install @aliaksandarpratashchyk/ieQuick start
import ie from '@aliaksandarpratashchyk/ie';
const userType = ie.object({
id: ie.string,
age: ie.optional.number,
tags: ie.array.string,
});
const user = {
id: '42',
age: undefined,
tags: ['admin'],
};
if (!userType.is(user)) throw new Error('Invalid user');
const jsonSnapshot = userType.to.json(user);
const rehydrated = userType.from.json(jsonSnapshot);Conversions
Each built-in type lists which targets it supports (for example, ie.number.from.string('42') or ie.bigint.to.string(42n)):
import ie from '@aliaksandarpratashchyk/ie';
const n = ie.number.from.string('42');
const s = ie.bigint.to.string(42n);Decorators
Attribute helpers let you attach runtime types to class properties or parameters:
import ie from '@aliaksandarpratashchyk/ie';
import { number, optional, string } from '@aliaksandarpratashchyk/ie/attributes';
class User {
@string
name!: string;
@number
score!: number;
@optional.string
bio?: string;
}
const userType = ie.constructable(User);Built-in types
- Primitives:
ie.string,ie.number,ie.boolean,ie.bigint,ie.date,ie.null,ie.undefined - Binary:
ie.binary(aUint8Array/ Node.jsBuffer;ie.binary.to.jsonuses base64) - Wrappers:
ie.optional(type)/ie.nullable(type)(also available asie.optional.string,ie.nullable.number, ...) - Collections:
ie.array(type),ie.tuple(...),ie.union(...),ie.object(schema) - Literals/enumerations:
ie.literal(value),ie.enumeration(...) - Classes:
ie.constructable(SomeClass)
See ie.api.md for the full public surface (generated by API Extractor).
Scripts
npm test— run the Jest suite, regenerateCOVERAGE.md, and updatecoverage.svg.npm run build— bundle the library and regenerate API docs (docs/).npm run lint/npm run format— lint and format the codebase.
Publishing
GitHub Actions runs tests on pushes/PRs and publishes to npm when the version in package.json changes on main. Set NPM_TOKEN in repository secrets to enable publishing.
