Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying class decorator affects valid ordering of class definitions (w/ emitDecoratorMetadata) #57937

Closed
johansenja opened this issue Mar 25, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@johansenja
Copy link

🔎 Search Terms

"ReferenceError: Cannot access class before initialization", "experimentalDecorators", "experimentalDecorators before initialization", "emitDecoratorMetadata", "emitDecoratorMetadata initialization", "emitDecoratorMetadata ordering"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about decorators, ordering and initialization, emitDecoratorMetadata

⏯ Playground Link

https://www.typescriptlang.org/play?noImplicitAny=false&strictNullChecks=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&alwaysStrict=false&noLib=true&removeComments=true&experimentalDecorators=true&emitDecoratorMetadata=true&target=5&jsx=0&module=1&inlineSources=true&isolatedModules=false&ts=5.5.0-dev.20240325#code/JYWwDg9gTgLgBAbzgSQHYCsCmBjGBDAIwBtM4BfOAMyghDgCIABVTAZxnVYHptaQJU9ANwAoEYzRZchEgAoAlCMwAPSLDjYieVqzgBBRCLgaB7KAFdc0WWCjAAbnhikomPABMBRAJ5wCALjgAIXlEMhFw8UkcfGJMBSVVaHhNbV0gw2NeVDNLGGtbBycXN09UHw1As2BUAHNQhHCyIA

💻 Code

import { Injectable } from "@nestjs/common";

@Injectable()
export class A {
  constructor(private readonly b: B) {}
}

@Injectable()
export class B {
  constructor(private readonly c: string) {}
}

🙁 Actual behavior

When running the output file node out.js you get this error:

out.js:22
    __metadata("design:paramtypes", [B])
                                     ^

ReferenceError: Cannot access 'B' before initialization

Emitted JS is as follows:

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.B = exports.A = void 0;
const common_1 = require("@nestjs/common");
let A = class A {
    constructor(b) {
        this.b = b;
    }
};
exports.A = A;
exports.A = A = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [B])
], A);
let B = class B {
    constructor(c) {
        this.c = c;
    }
};
exports.B = B;
exports.B = B = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [String])
], B);

In particular this part:

exports.A = A = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [B])
], A);

which references B before it is defined. But if I remove the @Injectable() decorator from class A, the JS output is valid because the __decorate block mentioned above is not generated.

🙂 Expected behavior

I would probably expect 1 of two things:
1/ TS would emit valid JS irrespective of whether the decorator is applied
or
2/ TS would emit an error against constructor(private readonly b: B) {} during compilation (saying that B is not yet defined) - if emitDecoratorMetadata is enabled

Additional information about the issue

Tested against: 5.3, 5.4, v5.5.0-dev.20240325
Other setings: ES2018 output, CommonJS module, emitDecoratorMetadata: true, experimentalDecorators: true

The easy workaround here is to re-order the classes so that B comes before it's mentioned in A's constructor:

import { Injectable } from "@nestjs/common";

@Injectable()
export class B {
  constructor(private readonly c: string) {}
}

@Injectable()
export class A {
  constructor(private readonly b: B) {}
}

but this took a bit of time to realised because it's not initially obvious that it's the Injectable() that's affecting the order you can define the classes/types in - ie. normally you can reference types before initialization just fine

type O = {
  c: C; // OK
}
class C {}
@MartinJohns
Copy link
Contributor

Duplicate of #43981.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 25, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants