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.

Generics (templates) are lost in type annotations within methods

See original GitHub issue

Running 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
blicklycommented, Jul 30, 2018

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.

1reaction
ChadKillingsworthcommented, Jul 29, 2018

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:

function MixedInterface() {}
/** @type {function(boolean):boolean} */ MixedInterface.prototype.foo;

/** @param {function(new:HTMLElement)} Superclass */
function mixFoo(Superclass) {
  /** @implements {MixedInterface} */
  class Foo extends Superclass {
    foo(test) { return test; }
  }
  return Foo;
}

/**
 * @constructor
 * @extends {HTMLElement}
 * @implements {MixedInterface}
 */
const FooElement = mixFoo(HTMLElement);

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).

Read more comments on GitHub >

github_iconTop 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 >

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