You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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:
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:
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:
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.
The text was updated successfully, but these errors were encountered: