0.1.2 • Published 5 years ago
json-to-typing v0.1.2
json-to-typing
Transform json data to Typescript type elements. If do not use Typescript and need to cover type of json data, this tool will be helpfuly for you. You can also use in terminal to automate the transfom process as cli command.
Install
Npm:
$ npm i json-to-typing --saveYarn:
$ yarn add json-to-typingExamples:
Basic
import jsonToTyping from 'json-to-typing';
jsonToTyping('{"name": "John", "age": 30, "isActive": true}');Result:
{
name: string;
age: number;
isActive: boolean;
}Nested
{
"name": "John",
"age": 30,
"isActive": true,
"books": ["Book 1", "Book 2", "Book 3"],
"movies": {
"genres": ["sci-fi", "comedy", "fantastic"],
"favorite": "The Lord Of The Rings"
}
}jsonToTyping(nested);Result:
{
name: string;
age: number;
isActive: boolean;
books: [string, string, string];
movies: {
genres: [string, string, string];
favorite: string;
}
}CLI
Install Globally
$ npm i -g json-to-typing$ json2typing --helpRun locally
$ node_modules/.bin/json2typing --helpRun with npx
$ npx json2typing --helpExamples
Basic:
$ json2typing --source '{"name": "John"}'output:
{
name: string;
}Interface Example:
$ json2typing interface IUser --source '{"name": "John"}'output:
interface IUser {
name: string;
}Note: You can use
--exportflag to add export keyword.
$ json2typing interface IUser --source '{"name": "John"}' --exportoutput:
export interface IUser {
name: string;
}Typing types
| Name |
|---|
| interface |
| declare |
| type |
Flags
| Name | Description |
|---|---|
| --source | JSON string or json file path |
| --export | Add export keyword |
| --help | Show help menu |
Use in CMD Pipeline
$ echo '{"name": "John"}' | json2typing type Member --exportoutput:
export type Member {
name: string;
}Another:
$ cat ./package.json | json2typing declare Package --exportoutput:
export declare const Package: {
version: string;
license: string;
main: string;
typings: string;
bin: {
json2typing: string;
};
files: [string, string];
engines: {
node: string;
};
scripts: {
start: string;
build: string;
};
peerDependencies: {};
husky: {
hooks: {
'pre-commit': string;
'commit-msg': string;
};
};
prettier: {
printWidth: number;
semi: boolean;
singleQuote: boolean;
trailingComma: string;
};
name: string;
author: string;
module: string;
devDependencies: {
husky: string;
};
dependencies: {};
};Tests
$ npm testContributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.