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

[BUG] typescript-fetch produces types with unused imports and arguments

Open eTallang-politiet opened this issue 1 year ago • 3 comments

Bug Report Checklist

  • [ ] Have you provided a full/minimal spec to reproduce the issue?
  • [ ] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [ ] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When we create types through the openapi-generator-cli, the generated TypeScript files contains some unused imports and arguments. Among these are import { mapValues } from '../runtime'; at the top of each file, and ignoreDiscriminator as an unused argument to each ...FromJSONTyped function.

These unused imports and arguments become an issue when we try to build our app and do type checking through tsc. Since these files are imported throughout our app to use the types, there is no way to exclude them from type checking externally.

Is there any way to prevent the type generator to generate files with unused imports and arguments, or could the generated files perhaps be annotated with // @ts-nocheck at the top of the file to exclude them from the tsc type checker?

openapi-generator version

2.17.0

Suggest a fix

The issue can be fixed if the generated output-files could be annotated with // @ts-nocheck at the top of each file.

eTallang-politiet avatar Mar 03 '25 09:03 eTallang-politiet

Can confirm this behavior. I'm experiencing the same issue

jacobgavin avatar Mar 10 '25 15:03 jacobgavin

I have the same behaviorwith import { mapValues } from '../runtime';

Michael-AT-Corporation avatar Mar 15 '25 19:03 Michael-AT-Corporation

For those who encountered the same issue, I ended up to run a post-processing script to inject // @ts-nocheck into all .ts files in the generated folder:

// scripts/add-ts-nocheck.js
const fs = require('fs');
const path = require('path');

function addTsNocheck(dir) {
  for (const file of fs.readdirSync(dir)) {
    const filePath = path.join(dir, file);
    if (fs.statSync(filePath).isDirectory()) {
      addTsNocheck(filePath);
    } else if (file.endsWith('.ts')) {
      const content = fs.readFileSync(filePath, 'utf-8');
      if (!content.startsWith('// @ts-nocheck')) {
        fs.writeFileSync(filePath, `// @ts-nocheck\n${content}`);
      }
    }
  }
}

addTsNocheck('src/api/generated');

And run after generating files:

node scripts/add-ts-nocheck.js

Not the most elegant solution but it did the trick.

Lornz- avatar Jul 01 '25 03:07 Lornz-