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

additionalProperties shouldn't affect TypeScript types from JTDDataType #2405

Open
jaketrent opened this issue Apr 4, 2024 · 2 comments
Open

Comments

@jaketrent
Copy link

jaketrent commented Apr 4, 2024

What version of Ajv you are you using?
8.12.0

What problem do you want to solve?
I would like stronger type checking on types returned from JTDDataType.

What do you think is the correct solution to problem?
JTDDataType should return a type as though additionalProperties is always false.

The needs of the two scenarios are different:

  • In schema validation: Most of the time, it's ok if a schema contains extra information. Downstream consumers of a JS object can just ignore extra fields. They don't even have to know it's there.
  • In type-checking code: If an object is relied on to have certain fields, they must be present in the declared interface. If code relies on more than is the interface, TypeScript will assist the programmer and indicate a potential problem.

The JTDDataType utility tries to bridge two worlds: generating types from schemas, which is great. But adding & Record to the end of such generated types seems to break useful, default-case type checking.

There is a workaround, but it feels icky for what seems like the default case:

const STRONG_MY_JTD_SCHEMA = { ...MY_JTD_SCHEMA, additionalProperties: false }
type MyType = JTDDataType<typeof STRONG_MY_JTD_SCHEMA>

Will you be able to implement it?

Not sure.

Presumably, the & Record needs removed from the type that's output. But I'm unsure of the related consequences of such a change.

Thank you for the great tool and your consideration of this feedback.

@jasoniangreen
Copy link
Collaborator

jasoniangreen commented Apr 4, 2024

Isn't this what already happens.

Here I have a JTD schema describing an object with an a prop and only that. When I use JTDDataType and try to add b prop it gets the red underline and the error is Object literal may only specify known properties, and 'b' does not exist in type.

Screenshot 2024-04-04 at 23 36 52

Is this what you expect?

@jaketrent
Copy link
Author

jaketrent commented Apr 4, 2024

Hi @jasoniangreen Thanks for your attention.

Yes, the example and expected tsc error you describe make sense.

The issue I'm describing always involves additionalProperties: true.

I've created a little test file so that you can both build TS and exercise runtime validations. I've added comments for a couple test cases. I hope it's helpful in seeing what I'm pointing out:

https://github.com/jaketrent/demo-ajv-ts-issue

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

No branches or pull requests

2 participants