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

when decorators emit type information class ordering becomes important #43981

Closed
paul-marechal opened this issue May 6, 2021 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@paul-marechal
Copy link

Bug Report

When using decorators along with design type info, decorating a class that depends on another one implicitly adds a class definition ordering constraint. Not properly ordering those class definitions will result in a runtime crash, and the only way to know this is to know this issue before hand.

🔎 Search Terms

decorator initialization

🕗 Version & Regression Information

typescript 3.9.9 and 4.2.3

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Decorators

⏯ Playground Link

Playground link with relevant code

💻 Code

function someDecorator<T>(target: T): T {
    return target
}

@someDecorator
class A {
    constructor(arg?: B) {
        console.log('constructed A!', arg)
    }
}

class B {}

const a = new A()

Relevant generated JavaScript:

// [...]
let A = class A {
    constructor(arg) {
        console.log('constructed A!', arg);
    }
};
A = __decorate([
    someDecorator,
    // EARLY REFERENCE ISSUE: B is defined below, so the JS runtime will complain about not-initialized refs!
    __metadata("design:paramtypes", [B])
], A);
// LATE DEFINITION OF B:
class B {
}
// [...]

🙁 Actual behavior

[ERR]: can't access lexical declaration 'B' before initialization

🙂 Expected behavior

TypeScript to either emit code in the right way, or complain at build time to prevent the runtime issue.

@paul-marechal paul-marechal changed the title when decorator emit type information class ordering becomes important when decorators emit type information class ordering becomes important May 6, 2021
@MartinJohns
Copy link
Contributor

MartinJohns commented May 6, 2021

Duplicate of #27519. The error is different, the cause is the same.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 6, 2021
@typescript-bot
Copy link
Collaborator

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

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