@boostpack/joi-extract-type
v1.0.0
Published
TypeScript type extraction for Joi schemas via module augmentation
Maintainers
Readme
@boostpack/joi-extract-type
TypeScript type extraction for Joi schemas via module augmentation.
Problem
Joi validates data at runtime, but doesn't provide compile-time types for validated values. You end up duplicating your schema as a TypeScript interface:
const schema = Joi.object({ name: Joi.string().required(), age: Joi.number() });
// manual duplication
interface Config {
name: string;
age?: number;
}This package augments Joi's type definitions so TypeScript can infer the type directly from the schema — no duplication needed.
Installation
npm install @boostpack/joi-extract-typejoi >= 17 is required as a peer dependency.
Usage
Import the package once (side-effect import) to activate the type augmentation:
import '@boostpack/joi-extract-type';
import Joi from 'joi';
const schema = Joi.object({
host: Joi.string().required(),
port: Joi.number().default(3000),
debug: Joi.boolean().optional(),
});
type Config = Joi.extractType<typeof schema>;
// { host: string; port: number; debug?: boolean | undefined }Required vs optional
Fields are optional by default. Use .required() or .default() to make them required:
const schema = Joi.object({
name: Joi.string().required(), // required — always string
age: Joi.number(), // optional — number | undefined
role: Joi.string().default('user'), // has default — always string
});Arrays
const schema = Joi.object({
tags: Joi.array().items(Joi.string()).required(),
});
type Result = Joi.extractType<typeof schema>;
// { tags: string[] }Alternatives
const schema = Joi.alternatives(Joi.string(), Joi.number());
type Result = Joi.extractType<typeof schema>;
// string | numberAcknowledgements
Based on the original joi-extract-type by Tiago de Carvalho Miranda, with fixes from panzelva and dp-franklin.
License
MIT
