-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
type: object and additionalProperties: type: array causing is not assignable to 'string' index type #1055
Comments
Maybe this is just invalid? |
This is a tricky one. Let's take a simplified schema: Example:
type: object
properties:
totals:
type: number
additionalProperties:
type: string v6 generates: Example: {
totals?: number;
[key: string]: string | undefined;
}; while v5 generates: Example: {
totals?: number;
} & { [key: string]: string }; Note the difference - an intersection. While the v5 generated types compile, they're not actually useful, since trying to assign anything to that type will fail:
Following discussion from microsoft/TypeScript#17867... there's a misunderstanding on what the index signature here actually means. If you were to index an object of this type with an arbitrary string, what would you expect? You would expect So, what we probably want to generate is actually: Example: {
totals?: number;
[key: string]: string | undefined | number;
}; which works as on the tin. Index with an arbitrary string, and you get string, other than the case where the string happens to be Does that make sense? This is a genuine bug. |
@mitchell-merry excellent explanation! |
I'm experiencing this when trying to generate a wrapper for Creditsafe's API. The issue is at line 21764 of their spec. Simplified, it's: "ProblemDetails": {
"type": "object",
"properties": {
"title": "string"
},
"additionalProperties": {
"type": "object"
}
} The generator produces ProblemDetails: {
title?: string;
[key: string]: Record<string, never> | undefined;
}; which is not valid TypeScript. But afaict this is valid OpenAPI. Basically it's all or nothing. If you have The issue is that |
I agree this is probably the right fix. And it should be possible. |
I don't think it's currently possible to specify a type |
Yeah I’m wondering if reverting to the old behavior of an intersection + a TS helper (or it sounds crazy, but maybe a union, depending on how it behaves in practice) would yield better results until microsoft/TypeScript#4196 resolves (which may not be anytime soon). I think it’s at least worth exploring some (non-breaking) TS shenanigans here. |
This issue is stale because it has been open for 90 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
I'm seeing in our existing code we have a type object where we've defined additional properties as an array. When we run
openapi-typescript
the TypeScript produced doesn't compile. Should this work? Thanks for the help!openapi-typescript
6.2.0
14.17.6
macOS 13
Reproduction
schema.yaml
Expected result
Creates valid TypeScript that compiles.
Checklist
The text was updated successfully, but these errors were encountered: