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.

Generic types for es6 classes and getters

See original GitHub issue

I am trying to annotate a getter on an es6 class with a generic type, but I can’t seem to get it working. A simplified example of the problem is:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @language_out ES5
// ==/ClosureCompiler==

/** @template T */
class X {
  
  /** @param {T} x */
  constructor(x) {
    this.x = x;
  }

  /** @return {T} */
  get foo() {
    return this.x;
  }
}

Produces:

JSC_TYPE_PARSE_ERROR: Bad type annotation. Unknown type T at line 9 character 15
  /** @return {T} */

which is the annotation on the foo getter (the constructor seems to work fine)

(Checked with https://closure-compiler.appspot.com)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lauraharkercommented, Dec 4, 2018

This is fixed now

2reactions
MatrixFrogcommented, Dec 1, 2016

I think I promised someone I would fix this a while back, and then it turned out to be harder than I thought. I believe the @template annotation on the class gets copied as you’d expect, but the problem is that the getter lives outside of the class in the transpiled output:

/** @constructor @template T */
function X() {}

Object.defineProperties(X.prototype, {
  foo: {
    /** @return {T} */    // <- The compiler doesn't know what you mean by "T" here.
    get: function() { return this.x; }
  }
});

I suspect this will ultimately be fixed by having the typechecker understand ES6 classes (including getters and setters) directly instead of typechecking the output of the 6-to-5 conversion 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Classes - JavaScript - MDN Web Docs
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on...
Read more >
What are getters and setters for in ECMAScript 6 classes?
In Java, getters and setters allow a class to define a JavaBean. The point of getters and setters is that it allows the...
Read more >
Documentation - Classes - TypeScript
TypeScript offers full support for the class keyword introduced in ES2015. As with other JavaScript language features, TypeScript adds type annotations and ...
Read more >
typescript-cheatsheet - GitHub Pages
Abstract classes are base classes from which other classes may be derived. They may not be instantiated directly. Unlike an interface, an abstract...
Read more >
Advanced TypeScript 4.8 Concepts: Classes and Types
Class properties can have getters and setters. A getter lets you compute a value to return as the property value, while a setter...
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