typeconv icon indicating copy to clipboard operation
typeconv copied to clipboard

OpenAPI -> TypeScript adds wildcard prop to each type

Open sashafklein opened this issue 3 years ago • 2 comments

I'm using typeconv to convert some OpenAPI schemas into TypeScript interfaces. Generally working great, but I noticed one thing.

When I start with the below OpenAPI spec:

{
    components: {
      schemas: {
        Person: {
          type: "object",
          properties: {
            first_name: {
              type: "string",
              example: "Pam",
            },
            last_name: {
              type: "string",
              example: "Halpert",
            },
          },
          required: [],
          title: "Person",
        },
      },
    },
  };

It produces the following type:

export interface Person {
    first_name?: string;
    last_name?: string;
    [key: string]: any;
}

Almost perfect, except for that last line:

    [key: string]: any;

It's appending a wildcard accessor to my type, for some reason, essentially asserting that on Person, any string can access any value, which makes the type not particularly helpful.

Any ideas what this is happening? I could get rid of this line through string manipulation, but I wonder why it's happening, and if there's some option I'm missing.

sashafklein avatar May 26 '22 23:05 sashafklein

Add

additionalProperties: false

Person: {
          type: "object",
          properties: {
            first_name: {
              type: "string",
              example: "Pam",
            },
            last_name: {
              type: "string",
              example: "Halpert",
            },
          },
          additionalProperties: false,
          required: [],
          title: "Person",
        },

jdcookie avatar May 31 '22 04:05 jdcookie

It would be nice if there was a flag to control this... I know it's technically incorrect, but a lot of 3rd party schema omit specifying additionalProperties: false. If you're trying to use this library to improve developer productivity and reduce bugs when interactive with one of these api, then having [key: string]: any; defeats the usefulness of this library.

ChuckJonas avatar Jun 14 '22 01:06 ChuckJonas