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 from JSDoc aren't really generic

See original GitHub issue
/**
 * @constructor
 * @template {string} K
 * @template V
 */
function Multimap() {
    /** @type {Object<string, V>} TODO: Remove the prototype from the fresh object */
    this._map = {};
};

var Ns = {}
/** @type {Multimap<"a" | "b", number>} */
const map = new Multimap();
const n = map._map['hi']

Expected behavior: n: number

Actual behavior: n: any in 3.0; n: V in 3.1-dev.

Types resolved from functions are never properly generic, even that function has @template-specified type parameters; they’re only special-cased in a few places to produce a specific instantiation of a type. They should use the normal generic type machinery that Typescript does.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
LAC-Techcommented, Dec 20, 2021

We do not have general plans to replicate all of TS’s features in JS, since we think that most people who want TS’ features will eventually switch to TS anyway. The main user we care about for JSDoc is the one who never thinks about TS, or types for that matter.

I get that you have to prioritise, but it’s sad to hear.

I strongly prefer to write Javascript instead of Typescript just because it removes a building step. But I still “think about types”.

For me it’s about setting up simpler projects with less moving parts.

3reactions
michaelolofcommented, Mar 12, 2019

Thank you for replying.

I kind of understand where you’re coming from and I can probably tell this pattern is definitely not conventional.

My issue with the JSDoc approach, is that it can get really verbose. The above example is a very simple use case for generics.

Consider a typed pipe function like this in a typescript file.

export function pipe<T1, R>(
  fn1: (arg1: T1) => R,
  ...fns: Array<(a: R) => R>
): (arg1: T1) => R;

export function pipe<T1, T2, R>(
  fn1: (arg1: T1, arg2: T2) => R,
  ...fns: Array<(a: R) => R>
): (arg1: T1, arg2: T2) => R;

export function pipe<T1, T2, T3, R>(
  fn1: (arg1: T1, arg2: T2, arg3: T3) => R,
  ...fns: Array<(a: R) => R>
): (arg1: T1, arg2: T2, arg3: T3) => R;

export function pipe<T1, T2, T3, T4, R>(
  fn1: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R,
  ...fns: Array<(a: R) => R>
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R;

export function pipe<R>(
  fn1: (...args: any[]) => R,
  ...fns: Array<(a: R) => R>
): (a: R) => R {
  return fns.reduce((prevFn, nextFn) => value => nextFn(prevFn(value)), fn1);
}

I shudder to think of how this would be implemented with JSDoc. Even the TypeScript approach is still a bit to noisy. I’m in an environment where my team is still warming up to TypeScript and I’m scared seeing something like this could easily put them off.

For me i believe the biggest benefit of this .d.ts approach is hiding away all the noise that comes with complicated typings like this. There’s also the advantage of seamlessly sharing interfaces extending interfaces etc. without JSDocing your entire codebase.

Also you have to consider this is an approach that works already in TypeScript. It just breaks when using generics.

I hope you do consider it.

Great job with all the work @typescript.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSDoc: Generic type attributes are not correctly propagated to ...
Generic types are resolved correctly for member of the current class but not propagated to base classes and interface. See the following example:....
Read more >
JSDoc & Generic types. Typescript | by Anton Krinitsyn | Medium
In this article, I will explain how to use Typescript with JSDoc and show 2 ways to do generic types in your code....
Read more >
JSDoc return instance of generic type - Stack Overflow
In short, I'm looking to pass a generic type into a factory's constructor and have the factory return instances ...
Read more >
Generics - TypeScript
While using any is certainly generic in that it will cause the function to accept any and all types for the type of...
Read more >
JSDoc typings: all the benefits of TypeScript, with none of the ...
Wonderful, because types really do help the coding phase. They don't reduce bugs, because I have tests for that, and any typing mistakes...
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