Generic types for es6 classes and getters
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is fixed now
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: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 😦