You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: JSDoc Map generics assignment any unsafe
Code
classMyServer{constructor(){// new Map is Map<any, any>; cannot call Map constructor with new Map<string, string[]>/** @type {Map<string, string[]>} */this.functions=newMap()}}constmyServer=newMyServer()myServer.functions.get('foo')
Expected behavior:
Expected to be able to say what the Map contains when creating a new map. In typescript you can do new Map<string, string[]>() but theres no explicit generic parameter function invocation syntax for JSDoc
Actual behavior:
The type Map<any, any> exists. This fails the eslint rule of no-unsafe-assignment and this fails the no-any rule of tslint etc.
Note that doing something similar for arrays works because there's a special case inference for [] and it also works for Set because the constructor is defined differently and it has generic inference in the initialization statement.
I think the thing you're encountering has more to do with the fact that we don't look at the contextual types during inference for any invocation expression except for ordinary calls. For example, this works:
Though it really depends on the problem we're trying to solve. In this case, I think this would be fixed by broadening our inference process, but it's definitely the case that you can't explicitly pass type arguments in JS files.
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms: JSDoc Map generics assignment any unsafe
Code
Expected behavior:
Expected to be able to say what the Map contains when creating a new map. In typescript you can do
new Map<string, string[]>()
but theres no explicit generic parameter function invocation syntax for JSDocActual behavior:
The type
Map<any, any>
exists. This fails the eslint rule ofno-unsafe-assignment
and this fails theno-any
rule of tslint etc.Note that doing something similar for arrays works because there's a special case inference for
[]
and it also works forSet
because the constructor is defined differently and it has generic inference in the initialization statement.Playground Link: https://www.typescriptlang.org/play?useJavaScript=true#code/MYGwhgzhAECyCeBlApgJwG5ugbwFDWmAHsA7CAF1QFdhyjVoAKASh3wOgHoAqb6AAXLwADshywwwgDwVUASxIBzADTRZCxQG0AugD4AvtG6d2BcgAs5EAHQAzKiVpzSMALzQSyAO5xJLdvq4gbjEZOTQALZIaJgM7p4+CCgYaP5RybF2Dk4u1orI5IwA5LZEREXMuCHgUHDRKQx4BKGyNHQMLGwcXLwCQqI4AIKoqGDwMpQaBkYm3RZWWY7kzmTQ7joBQVUt4ekxWPHedRmplXsNizlkeQXFpeXMQA
Related Issues: Yes, in eslint typescript-eslint/typescript-eslint#2109
The text was updated successfully, but these errors were encountered: