question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class properties referencing the class get incorrect class value when using functions defined outside the class

See original GitHub issue

TypeScript Version: 2.8.3 (also attempted with typescript@next at 2018-04-21 23:13:22 UTC)

Search Terms:

  • “class properties”
  • “class props”
  • “class prop”
  • the title of this issue

Code

function f() {
  return new A();
}

class A {
  static x = f();
}

console.log(A.x);

Expected behavior:

The function f should be able to access the correct value of A (a function), not undefined, when called inside a class property initializer.

It should work the same way as this code:

function f() {
  return new A();
}

class A {
  static x;
}

A.x = f();

console.log(A.x);

Actual behavior:

The class property initializer call to f() sees the value of A as undefined because A is assigned to the result of an IIFE which has not completed yet:

function f() {
    return new A();
}
var A = /** @class */ (function () {
    function A() {
    }
    A.x = f();
    return A;
}());
console.log(A.x);

Compare with the Babel output for this code which is:

"use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function f() {
  return new A();
}

var A = function A() {
  _classCallCheck(this, A);
};

A.x = f();

console.log(A.x);

And references the correct value of A when f() is called.

Playground Link: https://www.typescriptlang.org/play/#src=function f() { return new A()%3B } class A { static x %3D f()%3B } console.log(A.x)%3B

Related Issues:

Closest issues I could find were still not terribly related:

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mhegazycommented, Apr 23, 2018

Our emit is correct for --target es6 and later. the emit for --target es5 is not correct, since the class is not bound until all static initializers are run. this is does not match the TC39 proposal.

For context we have changed the way classes re emitted in ES5 to support for better minification, undoing that would impact the minification scenario.

0reactions
wavebeemcommented, May 2, 2018

Gotcha. Thanks for the explanation.

On Tue, May 1, 2018, 18:25 Ron Buckton notifications@github.com wrote:

No, we use it to scope _super and to mark the class body (via /** @class */) that is used by typescript service consumers to do things like tree-shaking.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Microsoft/TypeScript/issues/23609#issuecomment-385837785, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKyrxM6rYh8GmNle8rw2NXldvm_3F_6ks5tuQsZgaJpZM4TeqMx .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Classes - JavaScript - MDN Web Docs
It's an error to reference private fields from outside of the class; they can only be read or written within the class body....
Read more >
Property Access Methods - MATLAB & Simulink - MathWorks
If performance-critical access to properties occurs inside methods of the class, define private properties to store values. Use these values inside methods ...
Read more >
javascript - ES6 class variable alternatives - Stack Overflow
Properties specified in a class definition are assigned the same attributes as if they appeared in an object literal.
Read more >
Documentation - Classes - TypeScript
You can add parameters with type annotations, default values, and overloads: ts. class Point { ... A function property on a class is...
Read more >
How To Use Classes in TypeScript | DigitalOcean
When working with classes, most of the time you will need to create a constructor function. A constructor is a method that runs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found