lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.48k stars 498 forks source link

Support verbatimModuleSyntax and ESM modules properly #1669

Open blipk opened 1 month ago

blipk commented 1 month ago

Sorting

Expected Behavior

Routes should be compiled to:

import type { TsoaRoute } from '@tsoa/runtime';
import { fetchMiddlewares, ExpressTemplateService } from '@tsoa/runtime';

Current Behavior

Routes imports are compiled to

import { TsoaRoute, fetchMiddlewares, ExpressTemplateService } from '@tsoa/runtime';

Possible Solution

Suggestions in similar issues: https://github.com/lukeautry/tsoa/issues/1418 https://github.com/lukeautry/tsoa/issues/1628

Detailed Description

I spent a while trying to get my project to bundle using tsup and the issue was that I was importing TsoaResponse directly rather than as a type in my controller - like so:

import { Get, Route, Tags, Post, Body, Path, Response, SuccessResponse, Controller, Res, TsoaResponse } from "tsoa"

// should have been seperately
// import type { TsoaResponse } from "tsoa"

This was causing tsup to bundle the import and seeing as TSOA uses CJS and my packages uses ESM it wasnt able to find it and I couldnt run my built package - I wasted far too long trying to figure this out.

Normally I would catch these problems with my eslint rule: "@typescript-eslint/consistent-type-imports" : [ "error", { "prefer": "type-imports", "fixStyle": "separate-type-imports" } ],

However that rule doesnt work in file that uses decorators (like a controller) if experimentalDecorators and emitDecoratorMetadata are both set to true in tsconfig.json - which are required by tsoa. This is mentioned in the caveat here: https://typescript-eslint.io/rules/consistent-type-imports

Instead the verbatimModuleSyntax: true option at least shows me an error in the IDE which would have saved a lot of debugging time, and also should really be the standard, as if you're exporting as a type it should really only be imported as a type.

It would be nice to atleast have an option to properly seperate the type imports in the built routes, or to convert tsoa ESM.

github-actions[bot] commented 5 days ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days