OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-angular] Unused imports are generated that cause TS errors with "noUnusedLocals" enabled
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
Description
If you use typescript-angular the generated code might contain imports that are unused.
If the tsconfig configuration noUnusedLocals is enabled and you directly consume generated OpenAPI code within an Angular application, the TypeScript compiler errors:
✘ [ERROR] TS6133: 'FooDto' is declared but its value is never read. [plugin angular-compiler]
In my repo this occurs on model files with discriminators.
openapi-generator version
7.10.0
OpenAPI declaration file content or url
see https://github.com/jase88/openapi-ts-ng-unused-locals/blob/main/api-definition.openapi.yaml
Generation Details
MacOS 15.1
openjdk 23.0.1 2024-10-15
Node v22.8.0
Steps to reproduce
- checkout https://github.com/jase88/openapi-ts-ng-unused-locals/tree/main
- run
npm iandnpm run openapi:generate - run
npm run buildto start a Angular build that errors
Related issues/PRs
- https://github.com/OpenAPITools/openapi-generator/pull/16297
- https://github.com/OpenAPITools/openapi-generator/pull/12591
- https://github.com/OpenAPITools/openapi-generator/issues/1880
Suggest a fix
Ideally, it can be determined during generation that the import is not required in the file and can be discarded.
Alternatively, you could add @ts-check to every import, but in my opinion this should be a last resort.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked reproduction repository, run npm i, npm run openapi:generate, and npm run build to reproduce the TS6133 errors. Compare the generated discriminator model files and trace them back to the typescript-angular generator templates; done means generation no longer emits unused imports and the Angular build succeeds with noUnusedLocals enabled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100