-
-
Notifications
You must be signed in to change notification settings - Fork 531
Required object field with additionalProperties does not generate a required TypeScript field #1018
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
Comments
Any update on this? I've been bumping up against it, and right now am just grepping occurrences in the output TS, which is obviously sub-optimal. |
This behavior was a decision made to improve type safety because most users don’t have noUncheckedIndexAccess enabled, and this library can’t check that’s set. But when dealing with This behavior won’t be changed, but perhaps this a flag could be added if you’re assuming ownership of object index access and are confident you’re handling all the null cases properly. |
Understood, thanks for the explanation. My issue is that I'm actually only using the resulting object as an iterator, so of course It would be nice if Typescript was smart enough to somehow handle both (know that the value is defined when iterating over the object directly, but warn about possibly undefined when accessing by key), but I guess not? |
Oh yup—when you’re letting the object list its own keys you’re right this doesn’t affect you. But in all other instances, this is unsafe, and like you pointed out, TS can’t tell the difference. This behavior is actually my biggest gripe with TypeScript 😄. |
I don’t think we’d ever add a flag that disallows some part of your schema; IMO that gets into linting and the Redocly CLI is so good I’d probably just encourage users to take advantage of that if desired (you can easily write your own lint rules like this) However, I am starting to regret departing from default TypeScript behavior just because “that’s the way it should be.” On the one hand, there is a lot of gray area when converting OpenAPI → TypeScript (more than I imagined). Because they are different languages with different rules, more is a “best approximation” than most people realize. However, generating non-standard TS like this may come with more downsides than upsides. Even if it does help type safety for some, the end result usually does not behave as expected, especially when you get into complex composition ( Though if we did remove the |
@RWOverdijk ah, I see. Yes this original issue was over |
@drwpow I'll just crawl back in shame. It was a PEBKAC. I'll clear out my other issues. |
I support the removal of I don't think it's really a breaking change. Anyone that is already handling the |
The new release |
Bump, any update on this? :) |
Revisiting upgrading to v7 today, still seems to be an issue. |
Yes, still an issue |
@sorenhoyer This issue is resolved for me as of #1799 (v7.3.0). What version are you on and can you please elaborate on the issue? |
@psychedelicious using 7.4.1.
Only description and details will be required. But ideally all the properties that arent nullable should be required a well. Don't ask me why our devs put sku under properties :D But still... I'd expect and non-nullable property to be required. |
This issue is stale because it has been open for 180 days with no activity. If there is no activity in the next 7 days, the issue will be closed. |
This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem. |
Description
If a payload has a required object field whose
additionalProperties
specifies a schema for the values, the generated type shouldn't allowundefined
values.OpenAPI
The real schema where I'm hitting this is here but this toy schema demonstrates the problem.
Generated TS
Expected TS
Same as above but without the
| undefined
.Checklist
The text was updated successfully, but these errors were encountered: