Skip to content

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

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
anonbergine opened this issue Apr 10, 2023 · 4 comments · Fixed by #53742
Closed

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

anonbergine opened this issue Apr 10, 2023 · 4 comments · Fixed by #53742
Assignees
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Fix Available A PR has been opened for this issue

Comments

@anonbergine
Copy link

anonbergine commented Apr 10, 2023

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 .

@RyanCavanaugh
Copy link
Member

I can't repro this in TS 5.0.2. or nightly. Can you try installing the TS/JS nightly extension and seeing if that fixes it?

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Apr 10, 2023
@anonbergine
Copy link
Author

I can't repro this in TS 5.0.2. or nightly. Can you try installing the TS/JS nightly extension and seeing if that fixes it?

I've installed that extension and restarted VS Code and still getting the error. I'll try and make a smaller test case exhibiting the error.

@anonbergine
Copy link
Author

anonbergine commented Apr 10, 2023

I've updated code sample in OP to minimal code that reproduces the error.

Specifically, it seems to occur when a method of superclass Map is overriden.

EDIT: If I remove the jsdoc from the constructor the error dissapears.

@RyanCavanaugh RyanCavanaugh removed the Needs More Info The issue still hasn't been fully clarified label Apr 10, 2023
@RyanCavanaugh
Copy link
Member

Simpler:

/**
 * @template TValue
 */
export class StringKeyMap {
    /**
     * @constructor
     * @overload
     */
    constructor() {
    }

    /**
     * @param {TValue} value
     */
    bar(value) {
    }
}


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

Removing the @overload line makes the error go away, but this shouldn't have any effect

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation labels Apr 10, 2023
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.2.0 milestone Apr 10, 2023
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants