openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Types are not enforced with typescript `5.5.3`

Open tobiasdcl opened this issue 1 year ago • 6 comments

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

{
"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 ❤️

tobiasdcl avatar Jul 06 '24 15:07 tobiasdcl

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

aq1018 avatar Jul 11 '24 17:07 aq1018

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,

RPGillespie6 avatar Jul 17 '24 08:07 RPGillespie6

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

tobiasdcl avatar Jul 21 '24 09:07 tobiasdcl

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?

RPGillespie6 avatar Jul 21 '24 21:07 RPGillespie6

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 avatar Jul 24 '24 13:07 jimmymcpeter

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

jimmymcpeter avatar Jul 24 '24 14:07 jimmymcpeter

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.

sethetter avatar Jul 26 '24 11:07 sethetter

FYI: openapi-fetch v0.10.4 includes the fix

yukukotani avatar Jul 31 '24 02:07 yukukotani