Skip to content

emitDecoratorMetadata causes runtime errors by referencing type-only imports with namespaces #42624

Closed
@liorcode

Description

@liorcode

Bug Report

Related to #37672 and #39337 - seems this bug still happens if class is wrapped by a namespace.

🔎 Search Terms

emitDecoratorMetadata, type-only imports, decorators

🕗 Version & Regression Information

Typescript 4.1.3, Typescript 4.2.0 beta

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about emitDecoratorMetadata and type-only imports

⏯ Playground Link

Workbench Repro

💻 Code

// @experimentalDecorators
// @emitDecoratorMetadata
// @strict: false


// @filename: services.ts
export namespace Services {
  export class Service {}
}

// @filename: index.ts
import type { Services } from './services';

declare const decorator: any;
export class Main {
  @decorator()
  field: Services.Service;
}

🙁 Actual behavior

Typescript will reference "Services.Service" even though it's not imported (only the type was). This will crash in runtime:

__decorate([
decorator(),
__metadata("design:type", Services.Service)
], Main.prototype, "field", void 0);

🙂 Expected behavior

Should not reference non imported classes:

__decorate([
decorator(),
__metadata("design:type", Object)
], Main.prototype, "field", void 0);

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions