TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

TS 5.5 - Imports used by decorator output are removed

Open RomainLanz opened this issue 1 year ago • 2 comments

🔎 Search Terms

remove import compilation decorator

🕗 Version & Regression Information

  • This changed between versions 5.4 and 5.5

⏯ Playground Link

https://github.com/RomainLanz/typescript-output-issue

npm install
npm run build

💻 Code

import { DateTime } from "luxon";
import { column } from "@adonisjs/lucid/orm";

class User {
  @column.dateTime()
  declare createdAt: DateTime;
}

🙁 Actual behavior

When compiled the DateTime import is being removed (because it is only used as a type in our file), but the compilation of @column.dateTime() will use the import.

// ...

import { column } from "@adonisjs/lucid/orm";
class User {
}
__decorate([
    column.dateTime(),
    __metadata("design:type", typeof (_a = typeof DateTime !== "undefined" && DateTime) === "function" ? _a : Object)
], User.prototype, "createdAt", void 0);

🙂 Expected behavior

The import should be kept like in TypeScript 5.4.

import { DateTime } from "luxon";
import { column } from "@adonisjs/lucid/orm";
class User {
}
__decorate([
    column.dateTime(),
    __metadata("design:type", typeof (_a = typeof DateTime !== "undefined" && DateTime) === "function" ? _a : Object)
], User.prototype, "createdAt", void 0);

Additional information about the issue

No response

RomainLanz avatar Jun 27 '24 07:06 RomainLanz

Here is a discussion that may be related : https://github.com/microsoft/TypeScript/issues/54493#issuecomment-1615409830

RomainLanz avatar Jun 27 '24 07:06 RomainLanz

met this too

Max10240 avatar Jun 28 '24 04:06 Max10240

That means a project which enables following features at the same time will be unusable under TS 5.5:

  1. enable both emitDecoratorMetadata and experimentalDecorators in tsconfig.
  2. use declare keyword for class properties.
  3. verbatimModuleSyntax is false.

Some of my projects cannot upgrade to TS 5.5 too.

zanminkian avatar Jul 17 '24 13:07 zanminkian

Using https://www.npmjs.com/package/every-ts, this bisects to #58366. @weswigham

jakebailey avatar Jul 17 '24 16:07 jakebailey

It looks like this bit was added:

        if (location.flags & NodeFlags.Ambient) {
            return; // References within types and declaration files are never going to contribute to retaining a JS import
        }

Which explicitly skips marking references for declare.

jakebailey avatar Jul 17 '24 17:07 jakebailey