Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jahirfiquitiva opened this issue Jan 24, 2024 · 5 comments

Comments

@jahirfiquitiva
Copy link

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!

@sudhanshug16
Copy link

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

@jahirfiquitiva
Copy link
Author

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

@sudhanshug16
Copy link

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

@jahirfiquitiva
Copy link
Author

@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!

@bcherny bcherny closed this as completed Mar 2, 2024
@jahirfiquitiva
Copy link
Author

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants