microsoft / microsoft/TypeScript

Runtime error when a derived class has uninitialized decorated field and a constructor with nested super call

Open
#57,212 0 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Jan 29, 2024.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

decorator, initializer, constructor, super call

🕗 Version & Regression Information
  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried (after ES decorators are implemented), and I reviewed the FAQ for entries about decorators
  • I was unable to test this on prior versions because _______
⏯ Playground Link

https://www.typescriptlang.org/play?useDefineForClassFields=true&target=9&ts=5.4.0-dev.20240129#code/MYGwhgzhAEAiCmAnAlgN3gE2vAHgF3gDsMZRIYBvAX2goChpoABACgDMBXQ4PZAe0IsA+gBpowAQXwBKWg0bjJuPADowGDAElCyXmBDIAXknZce-QbPoKFeABbIIKtnz7QAvNAAMAbnmMqaXlA+Rc+AC5oMEIATz9GCUIIPEQOHj5EFit-aFQwRGgAI0h4D2gIDgAHE2l46Co6BrpEiD4QeBUQPgBzFkJ4AHc4JDRMLOdXaSA

💻 Code
class Derived extends class {} {
  @(function(_, context) {
    context.addInitializer(function() {
      this.foo = 0;
    })
  })
  foo: any;
  constructor() {
    var base = super();
  }
}

console.log(new Derived().foo)
🙁 Actual behavior

Runtime error: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

The generated code inserts __runInitializers calls before the nested super call. It seems that the transformer only searches top level super calls. We should probably traverse the constructor node.

🙂 Expected behavior

It should print 0

Additional information about the issue

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.