5.0.0 • Published 7 months ago
@lusc/json-dts v5.0.0
@lusc/json-dts
Convert JSON to TypeScript definitions (.d.ts).
Install
npm install @lusc/json-dts
# or using yarn
yarn add @lusc/json-dtsUsage
import {jsonDts} from '@lusc/json-dts';
jsonDts({key: 'value'});
/*
type T0 = {
key: string;
};
*/
jsonDts({key: {nested_key: 'value'}});
/*
type Key = {
nested_key: string;
};
type T0 = {
key: Key;
};
*/
jsonDts({key: true}, {name: 'data'});
/*
type Data = {
key: boolean
}
*/API
jsonDts(input, options?)
input
Anything that can be represented as JSON.
Has to be parsed JSON.
options.name?
Type: string
By default the the root item is called T0. This can be used to override that.
options.filter?
Type: string[] | (path: string[]) => boolean
This option can be used to filter entries from the output. Filtered entries will have type any.
For each key (including indices of arrays) found in input:
- If the filter is an array, it filters all keys found in that array.
- Otherwise it passes the path the function and filters that key if the function returns
false.
jsonDts(
{
a: 1,
b: 'b',
},
{
filter: ['a'],
},
);
// returns
type T0 = {
a: any;
b: string;
};
jsonDts(
{
a: {
b: 'b',
c: 1,
},
},
{
filter(path: string[]) {
// Filter *.b
return path?.[1] !== 'b';
},
},
);
// returns
type A = {
b: any;
c: number;
};
type T0 = {
a: A;
};5.0.0
7 months ago
4.2.3
9 months ago
4.2.2
9 months ago
4.2.1
10 months ago
4.1.2
10 months ago
4.2.0
10 months ago
4.1.1
1 year ago
4.1.0
2 years ago
4.0.2
2 years ago
4.0.1
2 years ago
3.1.1
2 years ago
4.0.0
2 years ago
3.1.0
2 years ago
3.0.0
3 years ago
2.0.0
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago