bcherny/json-schema-to-typescript

Option to label tuple elements

DrJonki opened this issue ยท 1 comments

I'm working on a custom RPC-like client-server communication layer, where I publish the back-end schema as JSON, and using this tool (thanks, BTW ๐Ÿ™‚) I generate TypeScript for the frontend code to utilize. It's working great, for the most part, but there's one thing missing that I would find very useful.


{
  "id": "test-schema",
  "items": [
    {
      "type": "number",
      "title": "NumberItem"
    },
    {
      "type": "string",
      "title": "StringItem"
    }
  ],
  "minItems": 2,
  "additionalItems": false
}

Currently the above schema is generated into this.

export type TestSchema = [NumberItem, StringItem];
export type NumberItem = number;
export type StringItem = string;

I would like to have the option to use tuple element labels instead of separate named types, so that the generated tuple would look like this instead of the above.

export type TestSchema = [numberItem: number, stringItem: string];

This would allow for better intellisense when the generated tuple is used as a type for a function spread parameter.
Untitled


I'd be willing to try to implement this if maintainers think it's a good idea.

This seems like a nice improvement. It would likely have to live behind a flag, since it's TS4+ which not everyone might be on.

We could say something like:

  • If inlineSimpleTupleMemberNames=true
  • And all the tuple members have titles defined
  • Then inline simple tuple types (we'd need to consider whether this inlining applies to booleans/numbers/strings only, or also to more complex types)

Let's keep this open to see if there's enough folks that need this to be worth the new flag.