bcherny/json-schema-to-typescript

Add option to remove `[k: string]: unknown | undefined;` from generated types

jahirfiquitiva opened this issue · 5 comments

Hi

I would like to make the generated types a bit more strict by removing the types:
[k: string]: unknown | undefined;

Is that possible? Otherwise, would you mind implementing such feature?
Thanks in advance!

I think you can use additionalProperties: false, to achieve this

I have it like that and it still generates them @sudhanshug16

Maybe you have additionalProperties: true in your JSON schema.

If that's the case and you really don't want to have that generic signature in your types, you can set additionalProperties to false on all the nodes, I am using this:

export function setNoAdditionalProps(schema: SchemaObject) {
  if (schema.type === "object") {
    schema.additionalProperties = false;
    for (const [, value] of Object.entries(schema.properties ?? {})) {
      setNoAdditionalProps(value as SchemaObject);
    }
  }
  if (schema.type === "array") {
    setNoAdditionalProps(schema.items as SchemaObject);
  }
}

@sudhanshug16 oh that's right, I was trying to use the schema from https://github.com/jsonresume/resume-schema/blob/master/schema.json and it does have additionalProperties: true

I will try this function later and let you know. Thank you so much!

@sudhanshug16 sorry it took me so long to get back to this, but I wanted to let you know your setNoAdditionalProps function worked fine 🙌 Thanks for the help!