UNPKG

2.12 kBMarkdownView Raw
1# joi2types
2
3[![codecov](https://codecov.io/gh/ycjcl868/joi2Types/branch/master/graph/badge.svg)](https://codecov.io/gh/ycjcl868/joi2Types) [![NPM version](https://img.shields.io/npm/v/joi2types.svg?style=flat)](https://npmjs.org/package/joi2types) [![NPM downloads](http://img.shields.io/npm/dm/joi2types.svg?style=flat)](https://npmjs.org/package/joi2types) [![CircleCI](https://circleci.com/gh/ycjcl868/joi2types/tree/master.svg?style=svg)](https://circleci.com/gh/ycjcl868/joi2types/tree/master) [![Install size](https://badgen.net/packagephobia/install/joi2types)](https://packagephobia.now.sh/result?p=joi2types)
4
5> a converter transforms @hapi/joi schema into TypeScript types.
6
7[Online demo](https://runkit.com/ycjcl868/joi2types)
8
9## Quick start
10
11Install
12
13```ts
14$ npm i joi2types @hapi/joi -S
15```
16
17use in your project
18
19```ts
20const Joi = require("@hapi/joi");
21const joi2Types = require("joi2types").default;
22
23// example for react-router-config
24const schema = Joi.array().items(
25 Joi.object({
26 path: Joi.string().description("Any valid URL path"),
27 component: Joi.string().description(
28 "A React component to render only when the location matches."
29 ),
30 redirect: Joi.string().description("navigate to a new location"),
31 exact: Joi.boolean().description(
32 "When true, the active class/style will only be applied if the location is matched exactly."
33 )
34 }).unknown()
35);
36
37(async () => {
38 const types = await joi2Types(schema, {
39 bannerComment: "/** comment for test */",
40 interfaceName: "IRoute"
41 });
42 console.log('types', types)
43})();
44```
45
46It will convert into types as follows:
47
48```ts
49/** comment for test */
50
51export type IRoute = {
52 /**
53 * Any valid URL path
54 */
55 path?: string;
56 /**
57 * A React component to render only when the location matches.
58 */
59 component?: string;
60 /**
61 * navigate to a new location
62 */
63 redirect?: string;
64 /**
65 * When true, the active class/style will only be applied if the location is matched exactly.
66 */
67 exact?: boolean;
68 [k: string]: any;
69}[];
70```
71
72## TODO
73
74- [ ] support custom type definitions using `tsType`