Skip to content

Overloaded constructors: 'TValue' not assignable to 'string' #53722

Closed
@anonbergine

Description

@anonbergine

Bug Report

🔎 Search Terms

constructor, overload, TValue, jsdoc

🕗 Version & Regression Information

Recent PR fixed the use of jsdoc-defined overloads for class constructors

That seems to have made it in to recent VS Code release; I'm using:

  • Version: 1.77.1 (user setup)
  • Commit: b7886d7461186a5eac768481578c1d7ca80e2d21
  • Date: 2023-04-04T23:21:11.906Z
  • Electron: 19.1.11
  • Chromium: 102.0.5005.196
  • Node.js: 16.14.2
  • V8: 10.2.154.26-electron.0
  • OS: Windows_NT x64 10.0.19045
  • Sandboxed: Yes

⏯ Playground Link

(Updated to exhibit error): Playground link

💻 Code

// @ts-check

/**
 * @template TValue
 * @type {StringKeyMap<TValue>}
 */
export class StringKeyMap extends Map {
    /**
     * @constructor
     * @overload
     * @param {Iterable<[string, TValue]>} iterable
     * @param {string} form
     *//**
     * @constructor
     * @overload
     * @param {string} form
     *//**
     * @constructor
     * @overload
     * @param {Iterable<[string, TValue]>} iterable
     *//**
     * @constructor
     * @overload
     */
    constructor(A, B) {
        super()
        // ...
    }

    /**
     * @override
     * @param {string} key
     * @param {TValue} value
     * @returns {this}
     */
    set(key, value) {
        // normalize key (removed for brevity)
        return super.set(key, value)
    }
}


/** @type {StringKeyMap<number>} */
const foo = new StringKeyMap() // <-- code hint error appears here

🙁 Actual behavior

In VS Code editor, if I try and instantiate my class using code like this:

class foo {
    // ...

    /** @type {StringKeyMap<string>} */
    #tags

    /**
     * @param {Iterable<string>} tags
     */
    constructor(tags) {
        this.#tags = new StringKeyMap() // <-- error shows here
    }

    // ....
}

This happens:

image

Hovering the error shows:

image

The code still works fine when used in node, the issue is constrained to the VS Code editor which seems to think TValue is a type rather than a template.

Possibly related to comment here about type checking being broken: #52477 (comment)

🙂 Expected behavior

TValue is a template and thus should adopt the @type {StringKeyMap<string>} defined for #tags .

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generationFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions