Skip to content

Commit 0f0115b

Browse files
authored
Set index type to all possible values (#348)
* test: create intersections for `additionalProperties` * fix: create intersections for `additionalProperties`
1 parent 5f4824a commit 0f0115b

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/v3.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,21 @@ export default function generateTypesV3(
9292

9393
let properties = createKeys(node.properties || {}, node.required);
9494

95-
// if additional properties, add to end of properties
96-
if (node.additionalProperties) {
97-
properties += `[key: string]: ${
98-
node.additionalProperties === true
99-
? "any"
100-
: transform(node.additionalProperties) || "any"
101-
};\n`;
102-
}
95+
// if additional properties, add an intersection with a generic map type
96+
const additionalProperties = node.additionalProperties
97+
? [
98+
`{ [key: string]: ${
99+
node.additionalProperties === true
100+
? "any"
101+
: transform(node.additionalProperties) || "any"
102+
};}\n`,
103+
]
104+
: [];
103105

104106
return tsIntersectionOf([
105107
...(node.allOf ? (node.allOf as any[]).map(transform) : []), // append allOf first
106-
...(properties ? [`{ ${properties} }`] : []), // then properties + additionalProperties
108+
...(properties ? [`{ ${properties} }`] : []), // then properties
109+
...additionalProperties, // then additional properties
107110
]);
108111
}
109112
case "array": {

tests/v3/expected/github.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ export interface paths {
8686
* response
8787
*/
8888
"201": {
89-
"application/json": components["schemas"]["integration"] & {
90-
client_id: string;
91-
client_secret: string;
92-
webhook_secret: string;
93-
pem: string;
94-
[key: string]: any;
95-
};
89+
"application/json": components["schemas"]["integration"] &
90+
({
91+
client_id: string;
92+
client_secret: string;
93+
webhook_secret: string;
94+
pem: string;
95+
} & { [key: string]: any });
9696
};
9797
"404": unknown;
9898
"422": unknown;
@@ -20980,8 +20980,7 @@ export interface components {
2098020980
metadata?: string;
2098120981
contents?: string;
2098220982
deployments?: string;
20983-
[key: string]: string;
20984-
};
20983+
} & { [key: string]: string };
2098520984
/**
2098620985
* The list of events for the GitHub app
2098720986
*/
@@ -20994,8 +20993,7 @@ export interface components {
2099420993
client_secret?: string;
2099520994
webhook_secret?: string;
2099620995
pem?: string;
20997-
[key: string]: any;
20998-
};
20996+
} & { [key: string]: any };
2099920997
/**
2100020998
* Basic Error
2100120999
*/

tests/v3/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ describe("OpenAPI3 features", () => {
291291
format(`
292292
export interface components {
293293
schemas: {
294-
additional_properties: { number?: number; [key: string]: any };
295-
additional_properties_string: { string?: string; [key: string]: string };
294+
additional_properties: { number?: number; } & { [key: string]: any };
295+
additional_properties_string: { string?: string } & { [key: string]: string };
296296
}
297297
}`)
298298
);

0 commit comments

Comments
 (0)