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:
- Created 5 years ago
- Reactions:23
- Comments:14 (3 by maintainers)
Top 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 >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 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.
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.
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.