grantila / typeconv

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
MIT License
421 stars 8 forks source link

OpenAPI -> TypeScript adds wildcard prop to each type #24

Open sashafklein opened 2 years ago

sashafklein commented 2 years ago

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.

jdcookie commented 2 years ago

Add

additionalProperties: false

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

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.