Typia
export function is<T>(input: unknown | T): input is T;
export function assert<T>(input: unknown | T): T;
export function validate<T>(input: unknown | T): IValidation<T>;
export function application<T>(): IJsonApplication;
export function assertParse<T>(input: string): T;
export function assertStringify<T>(input: T): string;
export function message<T>(): string;
export function assertDecode<T>(buffer: Buffer): T;
export function assertEncode<T>(input: T): Uint8Array;
export function random<T>(): Primitive<T>;
export function clone<T>(input: T): Primitive<T>;
export function prune<T extends object>(input: T): void;
typia
is a transformer library of TypeScript, supporting below features:
- Super-fast Runtime Validators
- Safe JSON parse and fast stringify functions
- Protocol Buffer encoder and decoder
- Protobuf/JSON schema generator
- Random data generator
All functions in typia
require only one line. You don't need any extra dedication like JSON schema definitions or decorator function calls. Just call typia
function with only one line like typia.assert<T>(input)
.
Also, as typia
performs AOT (Ahead of Time) compilation skill, its performance is much faster than other competitive libaries. For an example, when comparing validate function is()
with other competitive libraries, typia
is maximum 15,000x times faster than class-validator
.
Measured on Intel i5-1135g7, Surface Pro 8
Thanks for your support.
Your donation would encourage typia
development.
Setup
Transformation
npx typia setup
AOT (Ahead of Time) compilation mode.
When you write a TypeScript code calling typia.createIs<string | null>()
function and compile it, typia
will write optimal validation code like below, for the string | null
type. This is the transform mode performing AOT (Ahead of Time) compilation.
import typia from "typia";
export const check = typia.createIs<string | null>();
export const check = (input) => "string" === typeof input || null === input;
For reference, to use this transform mode, you've install one onf them; ttypescript or ts-patch.
If ttypescript, you should compile through ttsc
command, instead of using tsc
.
Otherwise, you've chosen ts-patch, you can use original tsc
command. However, ts-patch hacks node_modules/typescript
source code. Also, whenever update typescrtip
version, you have to run npm run prepare
command repeatedly.
By the way, when using @nest/cli, you must just choose ts-patch.
npx ttsc
npx ts-node -C ttypescript src/index.ts
tsc
npx ts-node src/index.ts
npm install --save-dev typescript@latest
npm run prepare
Generation
npm install --save typia
npx typia generate \
--input src/templates \
--output src/generated \
--project tsconfig.json
For frontend projects.
If you're using non-standard TypeScript compiler, you can't use transform mode.
- Non-standard TypeScript compilers:
Instead, you should utilize the generation mode.
Install typia
through npm install
command and run typia generate
command. Then, generator of typia
reads your TypeScript code of --input
, and writes transformed TypeScript code into the --output
directory, like below.
import typia from "typia";
export const check = typia.createIs<string | null>();
import typia from "typia";
export const check =
(input: unknown): input is string | null
=> "string" === typeof input || null === input;
Features
In here README documents, only summarized informations are provided.
For more details, refer to the Guide Documents (wiki).
- Runtime Validators
- Enhanced JSON
- Protocol Buffer
- Miscellaneous
Runtime Validators
export function is<T>(input: T | unknown): input is T;
export function assert<T>(input: T | unknown): T;
export function validate<T>(input: T | unknown): IValidation<T>;
export function equals<T>(input: T | unknown): input is T;
export function assertEquals<T>(input: T | unknown): T;
export function validateEquals<T>(input: T | unknown): IValidation<T>;
export function createIs<T>(): (input: unknown) => input is T;
export function createAssert<T>(): (input: unknown) => T;
export function createValidate<T>(): (input: unknown) => IValidation<T>;
export function createEquals<T>(): (input: unknown) => input is T;
export function createAssertEquals<T>(): (input: unknown) => T;
export function createValidateEquals<T>(): (input: unknown) => IValidation<T>;
typia
supports three type of validator functions:
is()
: returns false
if not matched with the type T
assert()
: throws a TypeGuardError
when not matchedvalidate()
Also, if you want more strict validator functions that even do not allowing superfluous properties not written in the type T
, you can use those functions instead; equals()
, assertEquals()
, validateEquals()
. Otherwise you want to create resuable validator functions, you can utilize factory functions like createIs()
instead.
When you want to add special validation logics, like limiting range of numeric values, you can do it through comment tags. If you want to know about it, visit the Guide Documents (Features > Runtime Validators > Comment Tags).
Enhanced JSON
export function application<
Types extends unknown[],
Purpose extends "swagger" | "ajv" = "swagger",
Prefix extends string = Purpose extends "swagger"
? "#/components/schemas"
: "components#/schemas",
>(): IJsonApplication;
export function isParse<T>(input: string): T | null;
export function assertParse<T>(input: string): T;
export function validateParse<T>(input: string): IValidation<T>;
export function stringify<T>(input: T): string;
export function isStringify<T>(input: T): string | null;
export function assertStringify<T>(input: T): string;
export function validateStringify<T>(input: T): IValidation<string>;
export function createAssertParse<T>(): (input: string) => T;
export function createAssertStringify<T>(): (input: T) => string;
typia
supports enhanced JSON functions.
application()
: generate JSON schema with only one line
- you can complement JSON schema contents through comment tags
assertParse()
: parse JSON string safely with type validationisStringify()
: maximum 10x faster JSON stringify fuction even type safe
Measured on AMD R7 6800HS
Protocol Buffer
export function message<T>(): string;
export function encode<T>(input: T): Uint8Array;
export function isEncode<T>(input: T): Uint8Array | null;
export function assertEncode<T>(input: T): Uint8Array;
export function validateEncode<T>(input: T): IValidation<Uint8Array>;
export function decode<T>(buffer: Uint8Array): T;
export function isDecode<T>(buffer: Uint8Array): T | null;
export function assertDecode<T>(buffer: Uint8Array): T;
export function validateDecode<T>(buffer: Uint8Array): IValidation<T>;
export function createDecode<T>(): (input: Uint8Array) => T;
export function createEncode<T>(): (input: T) => Uint8Array;
typia
supports Protocol Buffer.
message()
: generate Protocol Buffer schemaencode()
: encode JavaScript object to Protocol Bufferdecode()
: decode Protocol Buffer to JavaScript object
Do not need to define any *.proto
schema file. Just call above functions with generic argument T
, then typia
will generate the Protocol Buffer schema automatically, by analyzing your type T
.
If you want to add special type like float32
, you can do it through comment tags. If you want to know more about those comment tags, visit Guide Documents (Features > Protocol Buffer > Comment Tags).
Appendix
Nestia
Nestia is a set of helper libraries for NestJS
, supporting below features:
@nestia/core
: 15,000x times faster validation decorators@nestia/sdk
: evolved SDK and Swagger generators
- SDK (Software Development Kit)
- interaction library for client developers
- almost same with tRPC
nestia
: just CLI (command line interface) tool
Reactia
Not published yet, but soon
Reactia is an automatic React components generator, just by analyzing TypeScript type.
@reactia/core
: Core Library analyzing TypeScript type@reactia/mui
: Material UI Theme for core
and nest
@reactia/nest
: Automatic Frontend Application Builder for NestJS
When you want to automate an individual component, just use @reactia/core
.
import ReactDOM from "react-dom";
import typia from "typia";
import { ReactiaComponent } from "@reactia/core";
import { MuiInputTheme } from "@reactia/mui";
const RequestInput = ReactiaComponent<IRequestDto>(MuiInputTheme());
const input: IRequestDto = { ... };
ReactDOM.render(
<RequestInput input={input} />,
document.body
);
Otherwise, you can fully automate frontend application development through @reactia/nest
.
import React from "react";
import ReactDOM from "react-dom";
import { ISwagger } "@nestia/swagger";
import { MuiApplicationTheme } from "@reactia/mui";
import { ReactiaApplication } from "@reactia/nest";
const swagger: ISwagger = await import("./swagger.json");
const App: React.FC = ReactiaApplication(MuiApplicationTheme())(swagger);
ReactDOM.render(
<App />,
document.body
);