fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2629 from hey-api/feat/types-any

authored by

Lubos and committed by
GitHub
6e0f0603 4949a34e

+66 -6
+5
.changeset/happy-kangaroos-shake.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + feat(typescript): add `topType` option allowing to choose `any` over `unknown`
+14 -1
packages/openapi-ts-tests/main/test/3.1.x.test.ts
··· 70 70 input: 'additional-properties-true.json', 71 71 output: 'additional-properties-true', 72 72 }), 73 - description: 'allows arbitrary properties on objects', 73 + description: 'allows arbitrary properties on objects (unknown top type)', 74 + }, 75 + { 76 + config: createConfig({ 77 + input: 'additional-properties-true.json', 78 + output: 'additional-properties-true-any', 79 + plugins: [ 80 + { 81 + name: '@hey-api/typescript', 82 + topType: 'any', 83 + }, 84 + ], 85 + }), 86 + description: 'allows arbitrary properties on objects (any top type)', 74 87 }, 75 88 { 76 89 config: createConfig({
+3
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/additional-properties-true-any/index.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export * from './types.gen';
+23
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/additional-properties-true-any/types.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export type Foo = { 4 + foo: string; 5 + [key: string]: any | string; 6 + }; 7 + 8 + export type Bar = Foo & { 9 + [key: string]: any; 10 + }; 11 + 12 + export type Baz = Foo & { 13 + bar: string; 14 + [key: string]: any | string; 15 + }; 16 + 17 + export type Qux = { 18 + [key: string]: any; 19 + }; 20 + 21 + export type ClientOptions = { 22 + baseUrl: `${string}://${string}` | (string & {}); 23 + };
+1
packages/openapi-ts-tests/main/test/openapi-ts.config.ts
··· 169 169 // name: '我_responses_{{name}}', 170 170 // response: '他_response_{{name}}', 171 171 // }, 172 + topType: 'any', 172 173 // tree: true, 173 174 // webhooks: { 174 175 // name: 'Webby{{name}}Hook',
+1
packages/openapi-ts/src/plugins/@hey-api/typescript/config.ts
··· 12 12 case: 'PascalCase', 13 13 exportFromIndex: true, 14 14 style: 'preserve', 15 + topType: 'unknown', 15 16 tree: false, 16 17 }, 17 18 handler,
+3 -5
packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts
··· 39 39 }): ts.TypeNode => { 40 40 if (!schema.items) { 41 41 return tsc.typeArrayNode( 42 - tsc.keywordTypeNode({ 43 - keyword: 'unknown', 44 - }), 42 + tsc.keywordTypeNode({ keyword: plugin.config.topType }), 45 43 ); 46 44 } 47 45 ··· 332 330 if (schema.const && Array.isArray(schema.const)) { 333 331 itemTypes = schema.const.map((value) => { 334 332 const expression = tsc.valueToExpression({ value }); 335 - return expression ?? tsc.identifier({ text: 'unknown' }); 333 + return expression ?? tsc.identifier({ text: plugin.config.topType }); 336 334 }); 337 335 } else if (schema.items) { 338 336 for (const item of schema.items) { ··· 432 430 }); 433 431 case 'unknown': 434 432 return tsc.keywordTypeNode({ 435 - keyword: 'unknown', 433 + keyword: plugin.config.topType, 436 434 }); 437 435 case 'void': 438 436 return tsc.keywordTypeNode({
+16
packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts
··· 200 200 response?: StringName; 201 201 }; 202 202 /** 203 + * The top type to use for untyped or unspecified schema values. 204 + * 205 + * Can be: 206 + * - `unknown` (default): safe top type, you must narrow before use 207 + * - `any`: disables type checking, can be used anywhere 208 + * 209 + * @default 'unknown' 210 + */ 211 + topType?: 'any' | 'unknown'; 212 + /** 203 213 * Configuration for webhook-specific types. 204 214 * 205 215 * Controls generation of types for webhook payloads and webhook requests. ··· 429 439 */ 430 440 response: StringName; 431 441 }; 442 + /** 443 + * The top type to use for untyped or unspecified schema values. 444 + * 445 + * @default 'unknown' 446 + */ 447 + topType: 'any' | 'unknown'; 432 448 /** 433 449 * Configuration for webhook-specific types. 434 450 *