Skip to content

Cannot use generics in JSDoc object signature #27215

Closed

Description

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.

Activity

  1. ghost added
    BugA bug in TypeScript
    on Sep 19, 2018
  2. ghost added
    Domain: JSDocRelates to JSDoc parsing and type generation
    on Sep 19, 2018
  3. ghost added
    Domain: JavaScriptThe issue relates to JavaScript specifically
    on Sep 19, 2018
  4. ghost added
    checkJsRelates to checking JavaScript using TypeScript
    on Sep 19, 2018
  5. schmidtk commented on Oct 9, 2018

    @schmidtk
    Author

    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!

  6. sandersn commented on Oct 9, 2018

    @sandersn
    Member

    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.

  7. schmidtk commented on Oct 9, 2018

    @schmidtk
    Author

    Nathan Shively-Sanders (@sandersn) Excellent, appreciate the feedback. Thank you!

  8. sandersn commented on Oct 12, 2018

    @sandersn
    Member

    Duplicate of #26883

  9. Fearnbuster commented on Nov 22, 2019

    @Fearnbuster

    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.

  10. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

DuplicateAn existing issue was already created

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions