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
{{ message }}
Repository navigation
Cannot use generics in JSDoc object signature #27215
TypeScript does not appear to support using generics in JSDoc Object types (Object<string, T>). Doing so results in errors like Index signatures are incompatible. Type 'T' is not assignable to type 'string'..
TypeScript Version: 3.1.0-dev.20180919
Search Terms:
jsdoc object generic
object generic is not assignable to type
jsdoc object generic index signature
Code
/** * @param {T} val The value. * @return {Object<string, T>} The object. * @template T */constcreateGenericObject=function(val){return{'key': val};};/** * @const * @type {Object<string, string>} */constmyStringObject=createGenericObject('hello');/** * @const * @type {Object<string, number>} */constmyNumberObject=createGenericObject(42);
Expected behavior:
No errors.
Actual behavior:
objectgeneric.js:16:7 - error TS2322: Type '{ [x: string]: T; }' is not assignable to type '{ [x: string]: string; }'.
Index signatures are incompatible.
Type 'T' is not assignable to type 'string'.
16 const myStringObject = createGenericObject('hello');
~~~~~~~~~~~~~~
objectgeneric.js:22:7 - error TS2322: Type '{ [x: string]: T; }' is not assignable to type '{ [x: string]: number; }'.
Index signatures are incompatible.
Type 'T' is not assignable to type 'number'.
22 const myNumberObject = createGenericObject(42);
~~~~~~~~~~~~~~
Kevin Schmidt (@schmidtk) It's planned for 3.2, and next on my list after improving the infer-from-usage codefix (#27610). It's not a simple fix — JSDoc generics do not create actual generic types right now — so plan like you would for any software engineering estimate.
Activity
schmidtk commentedon Oct 9, 2018
Andy (Andrewkraft) (@Andy-MS) / Nathan Shively-Sanders (@sandersn): Is this something that could make the 3.2 milestone? I'm trying to get a rough estimate for planning purposes. Thanks for your time!
sandersn commentedon Oct 9, 2018
Kevin Schmidt (@schmidtk) It's planned for 3.2, and next on my list after improving the infer-from-usage codefix (#27610). It's not a simple fix — JSDoc generics do not create actual generic types right now — so plan like you would for any software engineering estimate.
schmidtk commentedon Oct 9, 2018
Nathan Shively-Sanders (@sandersn) Excellent, appreciate the feedback. Thank you!
sandersn commentedon Oct 12, 2018
Duplicate of #26883
Fearnbuster commentedon Nov 22, 2019
Does anyone have a guess as to why I'm still encountering these kind of errors?
I literally just copy and pasted the code from the original poster, and I'm getting errors that were identical to his.
I tried to install the latest version of Typescript (currently 3.7), but that didn't help anything.