mariusalch / json-to-ts Goto Github PK
View Code? Open in Web Editor NEWConvert jsons to typescript interfaces
Convert jsons to typescript interfaces
null is treated as a non existing member. This is not correct.
i.e.
JSON.parse('{ "something": [ {"x":1},{"x":"a"},{"x":null} ]}')
returns
{ something: [ { x: 1 }, { x: 'a' }, { x: null } ] }
So
{ "something": [ {"x":1},{"x":"a"},{"x":null} ]}
should produce
export interface RootObject {
something: Something[];
}
export interface Something {
x: number | string | null;
}
and not
export interface RootObject {
something: Something[];
}
export interface Something {
x?: number | string;
}
I'd love to use this tool on the commandline in a way like
npx json-to-ts source.json > interface.ts
I think there needs to be a flag for prepending export ...
to the interface to be able to import it from other files.
I'm using this in tests, to automatically write the interface to a file. But on subsequent runs the file gets overwritten. If I add export manually, it gets overwritten.
Thanks
I am trying to convert a selection in my code to interface, but it fails as single comma is not a valid identifier in JSON schema. But this will be super helpful if i could just select an object from the code and be able to generate typescript interface for it.
I have a use case where I don't want to pollute typescript's scope with additional types, I made a tiny hack wrapper that removes type duplication prevention but I think it'd be really cool if it was included in the library itself.
Also thanks for the awesome library.
This JSON, will cause
TypeError: Reduce of empty array with no initial value
at Array.reduce (<anonymous>)
at pascalCase (.../node_modules/json-to-ts/build/src/get-names.js:82:10)
I tried change
- .reduce(function (a, b) { return a + b; });
+ .reduce(function (a, b) { return a + b; }, '');
And it works.
I think this workaround is not good enough, there maybe a bigger problem inside.
If it's possible to add intellij plugin
It would be nice to support documentation for properties and entities, for example:
/** A Button is a clickable UI element */
interface Button {
/** The label will be shown when rendered */
label: string;
}
Yes, technically, JSON does not have a date type.
But I think it is a common use case to convert in memory JS object, which may contain a Date
object.
Right now it gets cast as object
.
interface Row {
period: string;
periodStartDay: object;
periodEndDay: object; // should be Date
}
I wish there was some kind of toggle for nullable fields. I don't need "null | " scattered throughout my definitions and an option to not generate that would be great.
The "Try it Online" link in the readme instructions is currently going to a massive malware site and should be removed/updated/fixed.
given this input
{
"alive": null
}
the expected output would be
interface RootObject {
alive: null;
}
but it actually produces
interface RootObject {
alive?: any;
}
Is there any way, with nested json structure, to have json-to-ts produce a single interface with the nesting rather than all the individual ones?
Example:
{
"Person": {
"name": "Bob",
"age": "10"
"Pet": {
"name": "Sprinkles",
"breed": "cat"
}
}
This would produce:
interface Person {
name: string,
age: string,
Pet: {
"name": string,
"breed": string
}
}
Instead of Person and Pet being two separate interfaces.
Thank you!
When generating definition files with json-to-ts (either in vs code or directly via the npm module), it puts the question mark for an optional key right inside the key string. Instead of outside of it.
See:
{
'features-CC_LMIV_25-Fanggerätkategorie_tlcm?': string[];
// (..) m?': (..) should be (..) m'?: (..)
}
Shouldn't it be:
'features-CC_LMIV_25-Fanggerätkategorie_tlcm'?: string[];
Some field names inside an interface are being declared with single quotes, while others aren't. Personally I prefer no quotes but either way consistency would be nice.
Example:
interface Game {
id: number;
season: number;
week: number;
day: string;
'visitor_team_id': number;
'visitor_team_abbrev': string;
'visitor_team_conference': null | string;
'visitor_team_division': null | string;
'visitor_team_name': string;
'visitor_team_full_name': string;
'home_team_id'?: any;
'home_team_abbrev': string;
'home_team_conference': null | string;
'home_team_division': null | string;
'home_team_name': string;
'home_team_full_name': string;
temperature: number;
'wind_direction'?: any;
'wind_speed'?: any;
humidity?: any;
conditions: string;
stadium?: any;
surface: string;
'over_under': number;
spread: number;
'visitor_score': number;
'home_score': number;
'is_playoff': boolean;
}
In the example:
const JsonToTS = require('json-to-ts')
const json = {
cats: [
{name: 'Kittin'},
{name: 'Mittin'}
],
favoriteNumber: 42,
favoriteWord: 'Hello'
}
JsonToTS(json).forEach( typeInterface => {
console.log(typeInterface)
})
I am in a situation where I would expect
interface Cat {
name: CatLiteralString;
}
rather than string
However I can understand situations where one would want string
rather than a literal...
So my thinking is maybe we could have an option allowing us to define keys that should be string literals?
for example
JsonToTS(json,{ literals: ['name'] })
which could maybe naively treat all string name
properties as literals ?
Thoughts?
Would be super useful if there was an option to sort keys in the interface.
I'd like if this service would provide a way to pass json code in the URL, similarly in an API way. Maybe having a REST API wouldn't be so bad either, I don't think any of the 'rivals' support that as of now.
I need to convert JSON data in typeORM entity.
Can you add this function to your library?
This is more a feature for the vs code extension specifically, but it would be nice if instead of pasting into the current file it would create a new file named after the interface per interface it parses out of a response. Especially if I could specify a root directory to put these interface definitions inside of.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.