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

Accessing readonly constructor parameters inside constructor while also extending a class gives error #2744

Closed
vitoke opened this issue Apr 26, 2023 · 1 comment · Fixed by #2757
Labels
bug Something isn't working regression

Comments

@vitoke
Copy link

vitoke commented Apr 26, 2023

What version of Bun is running?

0.6.0

What platform is your computer?

Darwin 22.4.0 arm64 arm

What steps can reproduce the bug?

Create a file with the following code:

class Parent {}

class Child extends Parent {
  calc: number;

  constructor(readonly value: number) {
    super();
    this.calc = value / 2;
  }
}

const a = new Child(6);
console.log(a.calc);

And run it with bun

What is the expected behavior?

Since this is valid TypeScript, the expected behavior is a console output of 3

What do you see instead?

I get the following output:

 5 | class Parent {}
 6 | 
 7 | class Child extends Parent {
 8 |   calc: number;
 9 | 
10 |   constructor(readonly value: number) {
                           ^
ReferenceError: 'super()' must be called in derived constructor before accessing |this| or returning non-object.
      at new Child (./src/index.ts:10:23)
      at ./src/index.ts:16:10

Additional information

When not extending another class, the code works fine. Also, when making the value property not readonly, the code works fine.

@vitoke vitoke added the bug Something isn't working label Apr 26, 2023
@Jarred-Sumner
Copy link
Collaborator

Looks like a minification bug. This happens because we default to enabling --minify-syntax in the runtime in v0.6. You can switch to 0.5.9 and it should work there.

 bun-debug build /tmp/abc.ts
// /tmp/abc.ts
class Parent {
}

class Child extends Parent {
  value;
  calc;
  constructor(value) {
    super();
    this.value = value;
    this.calc = value / 2;
  }
}
export {
  Child
};
 bun-debug build --minify-syntax /tmp/abc.ts
// /tmp/abc.ts
class Parent {
}

class Child extends Parent {
  value;
  calc;
  constructor(value) {
    this.value = value;
    super(), this.calc = value / 2;
  }
}
export {
  Child
};

0.5.9:

❯ ~/.bun/bin/bun build --platform=bun /tmp/abc.ts
class Parent {
}

export class Child extends Parent {
  value;
  calc;
  constructor(value) {
    super();
    this.value = value;
    this.calc = value / 2;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants