@loydjs/types
v1.1.0
Published
Loyd types — zero-runtime inference
Maintainers
Readme
Overview
@loydjs/types is a type-only package — it ships zero runtime code. It provides TypeScript utility types for inferring the input and output types of any Loyd schema, and helper types for building type-safe applications on top of Loyd.
Installation
npm install @loydjs/typesRequires
@loydjs/core· TypeScript ≥ 5.4 ·"strict": trueintsconfig.json
API
Infer<TSchema>
Infers the output type of a schema — the type of result.data after a successful safeParse.
import { object, string, number, array, optional } from "@loydjs/schema";
import type { Infer } from "@loydjs/types";
const UserSchema = object({
id: number().int().min(1),
name: string().minLength(2),
email: string().email(),
age: optional(number().int().min(0)),
roles: array(string()),
});
type User = Infer<typeof UserSchema>;
// {
// id: number
// name: string
// email: string
// age?: number | undefined
// roles: string[]
// }InferOutput<TSchema>
Alias for Infer. Explicit name for cases where you want to be clear about direction.
import type { InferOutput } from "@loydjs/types";
type UserOutput = InferOutput<typeof UserSchema>;
// same as Infer<typeof UserSchema>InferInput<TSchema>
Infers the input type — what the schema accepts before validation and transformation. Differs from the output type when transforms are applied (e.g. .trim(), .toLowerCase()).
import { string } from "@loydjs/schema";
import type { InferInput, InferOutput } from "@loydjs/types";
const NameSchema = string().trim().toLowerCase();
type NameInput = InferInput<typeof NameSchema>; // string
type NameOutput = InferOutput<typeof NameSchema>; // string (transformed)SchemaMap
The type used for object() shape definitions.
import type { SchemaMap } from "@loydjs/types";
import type { LoydSchema } from "@loydjs/core";
function buildForm<T extends SchemaMap>(shape: T) {
return object(shape);
}InferSchemaMap<TShape>
Infers the output type of an object shape directly.
import type { InferSchemaMap } from "@loydjs/types";
const shape = {
name: string(),
email: string().email(),
};
type FormData = InferSchemaMap<typeof shape>;
// { name: string; email: string }Usage patterns
API route types
import type { Infer } from "@loydjs/types";
const CreatePostSchema = object({
title: string().minLength(3).maxLength(200),
body: string().minLength(10),
tags: array(string()).max(10),
published: boolean(),
});
type CreatePostInput = Infer<typeof CreatePostSchema>;
async function createPost(data: CreatePostInput) {
// data is fully typed
const post = await db.posts.create(data);
return post;
}Generic validators
import type { Infer } from "@loydjs/types";
import type { LoydSchema } from "@loydjs/core";
import { safeParse } from "@loydjs/core";
function validate<T extends LoydSchema<unknown>>(
schema: T,
input: unknown
): Infer<T> {
const result = safeParse(schema, input);
if (!result.success) throw new Error(result.issues[0].code);
return result.data as Infer<T>;
}Dependencies
| Package | Role |
|:---|:---|
| @loydjs/core | LoydSchema base type for type inference |
@loydjs/typeshas zero runtime dependencies and ships no JavaScript.
Documentation
License
MIT © b3nito404
