openapi-ts / openapi-typescript

Generate TypeScript types from OpenAPI 3 specs
https://openapi-ts.dev
MIT License
5.47k stars 450 forks source link

Types are not enforced with typescript `5.5.3` #1749

Closed tobiasdcl closed 1 month ago

tobiasdcl commented 2 months ago

Description

types are not enforced with typescript 5.5.3

Reproduction

src/repro.ts

import createClient from 'openapi-fetch';
import type { paths } from 'petstore'; // generated via npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml -o src/petstore.d.ts

const client = createClient<paths>();

void client.POST('/store/order', {
  headers: {
    'Content-Type': 'application/json',
  },
  body: {
    // should result in "TS2322: Type 'boolean' is not assignable to type 'number'."
    id: true,
  },
});
tsconfig ### `tsconfig.json` ```json { "compilerOptions": { "lib": ["ES2023"], "module": "commonjs", "target": "ES2022", "strict": true, "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "sourceMap": true, "outDir": "./dist", "baseUrl": ".", "incremental": true, "skipLibCheck": true, "strictNullChecks": true, "noImplicitAny": true, "noUncheckedIndexedAccess": true, "strictBindCallApply": true, "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": false, "resolveJsonModule": true }, "include": [ "src/**/*", ], "exclude": ["node_modules", "dist"] } ```

Expected result

npx tsc --noEmit should result in something along the lines of:

repro.ts:12:5 - error TS2322: Type 'boolean' is not assignable to type 'number'.

12     id: true,

This does work as expected with typescript 5.4.5. If you run npx tsc --noEmit with typescript 5.5.3 no error is reported (which should not be the case). I saw that the project dependency on typescript is still on the 5.4 release. I am assuming there is some incompatibility with typescript 5.5 and this is one of the symptoms.
I took a quick look at the changed in TS 5.5 and found Control Flow Narrowing for Constant Indexed Accesses. I thought this might be related but it is just a gut feeling.

Checklist

Thanks for the great work ❤️

aq1018 commented 1 month ago

Upgraded to typescript 5.5.3 from 5.4.5 today, and found similar issue.

RPGillespie6 commented 1 month ago

I'm not able to repro with 5.5.3. I see:

$ npx tsc --version
Version 5.5.3
$ npx tsc --noEmit
test.ts:12:9 - error TS2322: Type 'boolean' is not assignable to type 'number'.

12         id: true,
tobiasdcl commented 1 month ago

I just tested again with the latest version of openapi-typescript (7.1.0) and openapi-fetch (0.10.2) - still not getting any error (except when using ts version 5.4.5).

I also added the tsconfig.json that I used - hope this helps to reproduce it

RPGillespie6 commented 1 month ago

Even with your tsconfig.json, I still can't repro... I'm using same versions of everything. Can you make a docker image that repros issue, or perhaps a github repo that repros issue in github actions?

Note my dev environment is Linux. Are you on Windows or Linux?

jimmymcpeter commented 1 month ago

I was struggling with this in a test node 20 project using @tsconfig/node20 tsconfig.json

I found if I deleted the entire "lib": [ "es2023" ], property, the type enforcement worked again.

jimmymcpeter commented 1 month ago

I am on Windows 11 Pro 22H2 using Node v20.10.0. Attached is my repo - repo.zip Hopefully this helps

sethetter commented 1 month ago

I am running into the same issue. Multipackage repo. Upgraded to typescript 5.5.4 and some valid type errors on arguments to calls of the openapi-fetch client methods disappeared.

yukukotani commented 1 month ago

FYI: openapi-fetch v0.10.4 includes the fix