GithubHelp home page GithubHelp logo

json-to-ts's People

Contributors

mariusalch avatar mbledkowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

json-to-ts's Issues

incorrect optional on null

given this input

{
  "alive": null
}

the expected output would be

interface RootObject {
  alive: null;
}

but it actually produces

interface RootObject {
  alive?: any;
}

Fails conversion from selection when single quotes are used

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.

Some interface field names are quoted, some are not

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;
}

VSCode extension creates new files per interface

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.

Add CLI

I'd love to use this tool on the commandline in a way like

npx json-to-ts source.json > interface.ts

Reduce of empty array with no initial value

https://pastebin.com/tnx6CH5a

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.

How to export the interfaces?

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

For optional property, question mark inside key quote instead of outside

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[];

One combined Interface instead of many smaller ones

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!

Not detecting date objects correctly

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
}

null is not handled correctly

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;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.