Generics (templates) are lost in type annotations within methods
See original GitHub issueRunning this command -
java -jar ../build/binaries/closure-compiler-v20180716.jar --js trial.js --language_in ECMASCRIPT_NEXT -O ADVANCED_OPTIMIZATIONS --jscomp_warning '*' --warning_level VERBOSE --js_output_file trial.min.js
With this trial.js -
/** @template T*/
class Helpers
{
/** @param {T} value
@return {!Promise<!Array<T>>} */
getValueAsArray(value)
{
return new Promise(
(resolve, reject) =>
{
/** @type {!Array<T>} */
const objects = [value];
resolve(objects);
});
}
}
/** @type {!Helpers<string>} */
const helpers = new Helpers();
helpers.getValueAsArray("hey").then(array => console.log(array));
Outputs -
trial.js:11: WARNING - Bad type annotation. Unknown type T
/** @type {!Array<T>} */
^
0 error(s), 1 warning(s), 92.0% typed
I realize the example looks weird. The actual scenario is that the value is not supplied in a parameter, but brought from IndexedDB -
/** @template T*/
class Store
{
/** @return {!Promise<!Array<T>>} */
getObjects()
{
return new Promise(
(resolve, reject) =>
{
/** @type {!Array<T>} */
const objects = [];
// Asynchronously get the stuff from IndexedDB
resolve(objects);
});
}
}
/** @type {!Store<string>} */
const store = new Store();
store.getObjects().then(array => console.log(array));
trial.js:10: WARNING - Bad type annotation. Unknown type T
/** @type {!Array<T>} */
^
0 error(s), 1 warning(s), 92.1% typed
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Generic Types · phan/phan Wiki - GitHub
Phan has primordial support for generic (templated) classes via type the annotations @template and @inherits and via a type syntax of the ...
Read more >Why are python Generic classes not enforcing their refined ...
"The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, ......
Read more >typing — Support for type hints — Python 3.11.1 documentation
Source code: Lib/typing.py This module provides runtime support for type hints. The most fundamental support consists of the types Any, Union, Callable, ...
Read more >PhpStorm 2021.2 Beta: Generics Support Is Coming
PhpStorm 2021.1 Beta is now available. We are adding preliminary support for generics in PHP. Let's get straight to it. Support for Generics...
Read more >Generic Types | Flow
Warning: Flow does not infer generic types. If you want something to have a generic type, annotate it. Otherwise, Flow may infer a...
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
I’m pretty sure that @concavelenz is working on an improvement to the typechecking that allows referring to template parameters inside the body of templated functions.
If you are looking for mixin support, I strongly recommend you use ES6 class mixin functions: http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
Supporting them in the compiler is a little bit interesting. You have to define an interface and mark the mixed constructor as implementing it:
This is supported by the compiler and type safe. One note - in ADVANCED mode the MixedInterface must be complete. It must contain all properties added to the prototype (even private ones).