Skip to content

Cannot use generics in JSDoc object signature #27215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
schmidtk opened this issue Sep 19, 2018 · 5 comments
Closed

Cannot use generics in JSDoc object signature #27215

schmidtk opened this issue Sep 19, 2018 · 5 comments
Assignees
Labels
Duplicate An existing issue was already created

Comments

@schmidtk
Copy link

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
 */
const createGenericObject = function(val) {
  return {
    'key': val
  };
};

/**
 * @const
 * @type {Object<string, string>}
 */
const myStringObject = createGenericObject('hello');

/**
 * @const
 * @type {Object<string, number>}
 */
const myNumberObject = 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);
         ~~~~~~~~~~~~~~

Playground Link: Not applicable (tsc behavior).

Related Issues: None found.

@schmidtk
Copy link
Author

schmidtk commented Oct 9, 2018

@Andy-MS / @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
Copy link
Member

sandersn commented Oct 9, 2018

@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
Copy link
Author

schmidtk commented Oct 9, 2018

@sandersn Excellent, appreciate the feedback. Thank you!

@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Oct 10, 2018
@sandersn
Copy link
Member

sandersn commented Oct 12, 2018

Duplicate of #26883

@sandersn sandersn marked this as a duplicate of #26885 Oct 12, 2018
@sandersn sandersn added the Duplicate An existing issue was already created label Oct 12, 2018
@sandersn sandersn marked this as not a duplicate of #26885 Oct 12, 2018
@sandersn sandersn marked this as a duplicate of #26883 Oct 12, 2018
@DanielRosenwasser DanielRosenwasser removed Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Domain: JavaScript The issue relates to JavaScript specifically Salsa checkJs Relates to checking JavaScript using TypeScript labels Oct 30, 2018
@DanielRosenwasser DanielRosenwasser removed this from the TypeScript 3.2 milestone Oct 30, 2018
@Fearnbuster
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants