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 #392 from hey-api/fix/prefer-unknown-over-any

fix: prefer unknown type over any

authored by

Jordan Shatford and committed by
GitHub
a2f68f6b a9a87831

+60 -46
+5
.changeset/few-rice-marry.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix: prefer unknown type over any
+9
docs/openapi-ts/migrating.md
··· 52 52 53 53 ## v0.39.0 54 54 55 + ### Prefer `unknown` over `any` 56 + 57 + Types that cannot be determined will now be generated as `unknown` instead of `any`. 58 + 59 + ```js 60 + 200: any // [!code --] 61 + 200: unknown // [!code ++] 62 + ``` 63 + 55 64 ### Single `enums.gen.ts` file 56 65 57 66 Enums are now exported from a separate file. If you use imports from `models.ts`, you can change them to `enums.gen.ts`.
+4 -4
packages/openapi-ts/src/openApi/v2/parser/getModel.ts
··· 15 15 ): Model => { 16 16 const model: Model = { 17 17 $refs: [], 18 - base: 'any', 18 + base: 'unknown', 19 19 description: definition.description || null, 20 20 enum: [], 21 21 enums: [], ··· 42 42 pattern: getPattern(definition.pattern), 43 43 properties: [], 44 44 template: null, 45 - type: 'any', 45 + type: 'unknown', 46 46 uniqueItems: definition.uniqueItems, 47 47 }; 48 48 ··· 120 120 121 121 if (definition.type === 'object') { 122 122 model.export = 'interface'; 123 - model.type = 'any'; 124 - model.base = 'any'; 123 + model.type = 'unknown'; 124 + model.base = 'unknown'; 125 125 126 126 if (definition.properties) { 127 127 const modelProperties = getModelProperties(openApi, definition, getModel);
+3 -3
packages/openapi-ts/src/openApi/v2/parser/getModelComposition.ts
··· 30 30 .filter(model => { 31 31 const hasProperties = model.properties.length; 32 32 const hasEnums = model.enums.length; 33 - const isObject = model.type === 'any'; 33 + const isObject = model.type === 'unknown'; 34 34 const isEmpty = isObject && !hasProperties && !hasEnums; 35 35 return !isEmpty; 36 36 }) ··· 69 69 if (properties.length) { 70 70 composition.properties.push({ 71 71 $refs: [], 72 - base: 'any', 72 + base: 'unknown', 73 73 description: '', 74 74 enum: [], 75 75 enums: [], ··· 83 83 name: 'properties', 84 84 properties, 85 85 template: null, 86 - type: 'any', 86 + type: 'unknown', 87 87 }); 88 88 } 89 89
+2 -2
packages/openapi-ts/src/openApi/v2/parser/getOperationParameter.ts
··· 13 13 export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => { 14 14 const operationParameter: OperationParameter = { 15 15 $refs: [], 16 - base: 'any', 16 + base: 'unknown', 17 17 description: parameter.description || null, 18 18 enum: [], 19 19 enums: [], ··· 41 41 prop: parameter.name, 42 42 properties: [], 43 43 template: null, 44 - type: 'any', 44 + type: 'unknown', 45 45 uniqueItems: parameter.uniqueItems, 46 46 }; 47 47
+2 -2
packages/openapi-ts/src/openApi/v2/parser/getOperationResponse.ts
··· 14 14 ): OperationResponse => { 15 15 const operationResponse: OperationResponse = { 16 16 $refs: [], 17 - base: responseCode !== 204 ? 'any' : 'void', 17 + base: responseCode !== 204 ? 'unknown' : 'void', 18 18 code: responseCode, 19 19 description: response.description || null, 20 20 enum: [], ··· 30 30 name: '', 31 31 properties: [], 32 32 template: null, 33 - type: responseCode !== 204 ? 'any' : 'void', 33 + type: responseCode !== 204 ? 'unknown' : 'void', 34 34 }; 35 35 36 36 // If this response has a schema, then we need to check two things:
+2 -2
packages/openapi-ts/src/openApi/v3/parser/getModel.ts
··· 130 130 131 131 if (definition.type === 'object' || definition.properties) { 132 132 if (definition.properties) { 133 - model.base = 'any'; 133 + model.base = 'unknown'; 134 134 model.export = 'interface'; 135 - model.type = 'any'; 135 + model.type = 'unknown'; 136 136 model.default = getDefault(definition, model); 137 137 138 138 const modelProperties = getModelProperties(openApi, definition, getModel, model);
+2 -2
packages/openapi-ts/src/openApi/v3/parser/getModelComposition.ts
··· 103 103 } else { 104 104 composition.properties.push({ 105 105 $refs: [], 106 - base: 'any', 106 + base: 'unknown', 107 107 description: '', 108 108 enum: [], 109 109 enums: [], ··· 117 117 name: 'properties', 118 118 properties, 119 119 template: null, 120 - type: 'any', 120 + type: 'unknown', 121 121 }); 122 122 } 123 123 }
+2 -2
packages/openapi-ts/src/openApi/v3/parser/getOperationParameter.ts
··· 12 12 export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => { 13 13 const operationParameter: OperationParameter = { 14 14 $refs: [], 15 - base: 'any', 15 + base: 'unknown', 16 16 deprecated: parameter.deprecated === true, 17 17 description: parameter.description || null, 18 18 enum: [], ··· 30 30 prop: parameter.name, 31 31 properties: [], 32 32 template: null, 33 - type: 'any', 33 + type: 'unknown', 34 34 }; 35 35 36 36 if (parameter.$ref) {
+2 -2
packages/openapi-ts/src/openApi/v3/parser/getOperationRequestBody.ts
··· 9 9 export const getOperationRequestBody = (openApi: OpenApi, body: OpenApiRequestBody): OperationParameter => { 10 10 const requestBody: OperationParameter = { 11 11 $refs: [], 12 - base: 'any', 12 + base: 'unknown', 13 13 default: undefined, 14 14 description: body.description || null, 15 15 enum: [], ··· 27 27 prop: body['x-body-name'] ?? 'requestBody', 28 28 properties: [], 29 29 template: null, 30 - type: 'any', 30 + type: 'unknown', 31 31 }; 32 32 33 33 if (body.content) {
+2 -2
packages/openapi-ts/src/openApi/v3/parser/getOperationResponse.ts
··· 15 15 ): OperationResponse => { 16 16 const operationResponse: OperationResponse = { 17 17 $refs: [], 18 - base: responseCode !== 204 ? 'any' : 'void', 18 + base: responseCode !== 204 ? 'unknown' : 'void', 19 19 code: responseCode, 20 20 description: response.description || null, 21 21 enum: [], ··· 31 31 name: '', 32 32 properties: [], 33 33 template: null, 34 - type: responseCode !== 204 ? 'any' : 'void', 34 + type: responseCode !== 204 ? 'unknown' : 'void', 35 35 }; 36 36 37 37 if (response.content) {
+2 -2
packages/openapi-ts/test/__snapshots__/test/generated/v2/models.gen.ts.snap
··· 584 584 /** 585 585 * Response is a simple number 586 586 */ 587 - 200: any; 587 + 200: unknown; 588 588 /** 589 589 * Success 590 590 */ ··· 778 778 /** 779 779 * Custom message: Successful response 780 780 */ 781 - 200: any; 781 + 200: unknown; 782 782 }; 783 783 }; 784 784 };
+5 -5
packages/openapi-ts/test/__snapshots__/test/generated/v2/services.gen.ts.snap
··· 316 316 } 317 317 318 318 /** 319 - * @returns any Response is a simple number 319 + * @returns unknown Response is a simple number 320 320 * @returns void Success 321 321 * @throws ApiError 322 322 */ ··· 333 333 334 334 export class ResponseService { 335 335 /** 336 - * @returns any Response is a simple number 336 + * @returns unknown Response is a simple number 337 337 * @returns void Success 338 338 * @throws ApiError 339 339 */ ··· 377 377 } 378 378 379 379 /** 380 - * @returns any Message for 200 response 380 + * @returns unknown Message for 200 response 381 381 * @returns ModelWithString Message for default response 382 382 * @returns ModelThatExtends Message for 201 response 383 383 * @returns ModelThatExtendsExtends Message for 202 response ··· 490 490 * @returns number Response is a simple number 491 491 * @returns string Response is a simple string 492 492 * @returns boolean Response is a simple boolean 493 - * @returns any Response is a simple object 493 + * @returns unknown Response is a simple object 494 494 * @throws ApiError 495 495 */ 496 496 public static types( ··· 576 576 577 577 export class ErrorService { 578 578 /** 579 - * @returns any Custom message: Successful response 579 + * @returns unknown Custom message: Successful response 580 580 * @throws ApiError 581 581 */ 582 582 public static testErrorCode(
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3/models.gen.ts.snap
··· 1363 1363 /** 1364 1364 * Custom message: Successful response 1365 1365 */ 1366 - 200: any; 1366 + 200: unknown; 1367 1367 }; 1368 1368 }; 1369 1369 };
+3 -3
packages/openapi-ts/test/__snapshots__/test/generated/v3/services.gen.ts.snap
··· 538 538 } 539 539 540 540 /** 541 - * @returns any Message for 200 response 541 + * @returns unknown Message for 200 response 542 542 * @returns ModelWithString Message for default response 543 543 * @returns ModelThatExtends Message for 201 response 544 544 * @returns ModelThatExtendsExtends Message for 202 response ··· 789 789 } 790 790 791 791 /** 792 - * @returns any OK 792 + * @returns unknown OK 793 793 * @throws ApiError 794 794 */ 795 795 public static multipartResponse(): CancelablePromise< ··· 824 824 825 825 export class ErrorService { 826 826 /** 827 - * @returns any Custom message: Successful response 827 + * @returns unknown Custom message: Successful response 828 828 * @throws ApiError 829 829 */ 830 830 public static testErrorCode(
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.gen.ts.snap
··· 1363 1363 /** 1364 1364 * Custom message: Successful response 1365 1365 */ 1366 - 200: any; 1366 + 200: unknown; 1367 1367 }; 1368 1368 }; 1369 1369 };
+3 -3
packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/services.gen.ts.snap
··· 585 585 } 586 586 587 587 /** 588 - * @returns any Message for 200 response 588 + * @returns unknown Message for 200 response 589 589 * @returns ModelWithString Message for default response 590 590 * @returns ModelThatExtends Message for 201 response 591 591 * @returns ModelThatExtendsExtends Message for 202 response ··· 877 877 } 878 878 879 879 /** 880 - * @returns any OK 880 + * @returns unknown OK 881 881 * @throws ApiError 882 882 */ 883 883 public multipartResponse(): Observable<$OpenApiTs['/api/v{api-version}/multipart']['get']['res'][200]> { ··· 918 918 constructor(public readonly http: HttpClient) {} 919 919 920 920 /** 921 - * @returns any Custom message: Successful response 921 + * @returns unknown Custom message: Successful response 922 922 * @throws ApiError 923 923 */ 924 924 public testErrorCode(
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.gen.ts.snap
··· 1363 1363 /** 1364 1364 * Custom message: Successful response 1365 1365 */ 1366 - 200: any; 1366 + 200: unknown; 1367 1367 }; 1368 1368 }; 1369 1369 };
+3 -3
packages/openapi-ts/test/__snapshots__/test/generated/v3_client/services.gen.ts.snap
··· 559 559 } 560 560 561 561 /** 562 - * @returns any Message for 200 response 562 + * @returns unknown Message for 200 response 563 563 * @returns ModelWithString Message for default response 564 564 * @returns ModelThatExtends Message for 201 response 565 565 * @returns ModelThatExtendsExtends Message for 202 response ··· 828 828 } 829 829 830 830 /** 831 - * @returns any OK 831 + * @returns unknown OK 832 832 * @throws ApiError 833 833 */ 834 834 public multipartResponse(): CancelablePromise<$OpenApiTs['/api/v{api-version}/multipart']['get']['res'][200]> { ··· 863 863 constructor(public readonly httpRequest: BaseHttpRequest) {} 864 864 865 865 /** 866 - * @returns any Custom message: Successful response 866 + * @returns unknown Custom message: Successful response 867 867 * @throws ApiError 868 868 */ 869 869 public testErrorCode(
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_date/models.gen.ts.snap
··· 595 595 /** 596 596 * Custom message: Successful response 597 597 */ 598 - 200: any; 598 + 200: unknown; 599 599 }; 600 600 }; 601 601 };
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.gen.ts.snap
··· 1363 1363 /** 1364 1364 * Custom message: Successful response 1365 1365 */ 1366 - 200: any; 1366 + 200: unknown; 1367 1367 }; 1368 1368 }; 1369 1369 };
+3 -3
packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/services.gen.ts.snap
··· 538 538 } 539 539 540 540 /** 541 - * @returns any Message for 200 response 541 + * @returns unknown Message for 200 response 542 542 * @returns ModelWithString Message for default response 543 543 * @returns ModelThatExtends Message for 201 response 544 544 * @returns ModelThatExtendsExtends Message for 202 response ··· 789 789 } 790 790 791 791 /** 792 - * @returns any OK 792 + * @returns unknown OK 793 793 * @throws ApiError 794 794 */ 795 795 public static multipartResponse(): CancelablePromise< ··· 824 824 825 825 export class ErrorService { 826 826 /** 827 - * @returns any Custom message: Successful response 827 + * @returns unknown Custom message: Successful response 828 828 * @throws ApiError 829 829 */ 830 830 public static testErrorCode(
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.gen.ts.snap
··· 1363 1363 /** 1364 1364 * Custom message: Successful response 1365 1365 */ 1366 - 200: any; 1366 + 200: unknown; 1367 1367 }; 1368 1368 }; 1369 1369 };