ts2typebox - npm
source link: https://www.npmjs.com/package/ts2typebox
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ts2typebox
Cli tool used to generate typebox JSON schemas based on given typescript types. Based on the initial codegen code from typebox by sinclairzx81.
Installation
npm i -g ts2typebox
Use Case
- You got a typescript project with types already lying around and are looking for a quick and easy way to get validations and JSON schemas based on them.
- You prefer leveraging typescript to define your data types and to generate your validators and/or JSON schemas.
Usage
- The cli can be used with
ts2typebox --input <fileName> --output <fileName>
, or by simply runningts2typebox
. The input defaults to "types.ts" and the output to "generated-types.ts" relative to the current working directory. For more see cli usage.
Examples
//
// Let's start with a simple type
//
export type Person = {
age: number;
name: string;
};
//
// Which becomes
//
import { Type, Static } from "@sinclair/typebox";
export type Person = Static<typeof Person>;
export const Person = Type.Object({
age: Type.Number(),
name: Type.String(),
});
//
// Similarly, with an interface
//
export interface IPerson {
age: number;
name: string;
}
//
// Which becomes
//
import { Type, Static } from "@sinclair/typebox";
export type IPerson = Static<typeof IPerson>;
export const IPerson = Type.Object({
age: Type.Number(),
name: Type.String(),
});
//
// Let's add some more complicated types. What about unions and intersections?
//
export type T = { x: number } & { y: number };
export type U = { x: number } | { y: number };
//
// Which becomes
//
import { Type, Static } from "@sinclair/typebox";
export type T = Static<typeof T>;
export const T = Type.Intersect([
Type.Object({
x: Type.Number(),
}),
Type.Object({
y: Type.Number(),
}),
]);
export type U = Static<typeof U>;
export const U = Type.Union([
Type.Object({
x: Type.Number(),
}),
Type.Object({
y: Type.Number(),
}),
]);
//
// Nice! But I will need some JSON schema options here and there, which can't be
// expressed in typescript at all...
// No worries, simply use jsdoc in your types!
//
/**
* @minimum 100
* @maximum 200
* @multipleOf 2
* @default 150
* @description "it's a number" - strings must be quoted
* @foobar "should support unknown props"
*/
export type T = number;
//
// Which becomes
//
import { Type, Static } from "@sinclair/typebox";
export type T = Static<typeof T>;
export const T = Type.Number({
minimum: 100,
maximum: 200,
multipleOf: 2,
default: 150,
description: "it's a number",
foobar: "should support unknown props",
});
//
// Well, what if I don't like that the generated type and value has the same
// name?! No worries, you can define your own transformation functions!
// So that this..
//
export type Person = {
age: number;
name: string;
};
//
// Can easily become
//
import { Type, Static } from "@sinclair/typebox";
export type PersonType = Static<typeof PersonSchema>;
export const PersonSchema = Type.Object({
age: Type.Number(),
name: Type.String(),
});
To cut the slack, all the standard types supported in typebox are supported by ts2typebox. For hands on examples, you can follow the simple snippets stored inside the examples folder. For a more complete set of examples (every feature is tested) you can take a look at the tests inside the repo.
cli usage
The following text is the output that will be displayed when you issue ts2typebox -h
or
ts2typebox --help
.
ts2typebox is a cli tool to generate typebox JSON schemas based on given
typescript types. The generated output is formatted based on the prettier
config inside your repo (or the default one, if you don't have one).
Version: ${packageJson.version}
Usage:
ts2typebox [ARGUMENTS]
Arguments:
-h, --help
Displays this menu.
-i, --input
Specifies the relative path to the file containing the typescript types
that will be used to generated typebox types. Defaults to "types.ts".
-o, --output
Specifies the relative path to generated file that will contain the
typebox types. Defaults to "generated-types.ts".
--output-stdout
Does not generate an output file and prints the generated code to stdout
instead. Has precedence over -o/--output.
Additional:
You can adapt the names of the generated types (as well as the names of the
generated values) using custom transformation functions which take a string
as an input and return a string as their output. These will run on each of
the generated types and values, respectively. Please take a look inside the
repo under ./examples/transform-value-transform-type for an example of this.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK