Skip to content

@overload doesn't work for constructors #52477

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 Jan 28, 2023 · 3 comments · Fixed by #52577
Closed

@overload doesn't work for constructors #52477

anonbergine opened this issue Jan 28, 2023 · 3 comments · Fixed by #52577
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.

Comments

@anonbergine
Copy link

Bug Report

🔎 Search Terms

"jsdoc overload"
"constructor overload jsdoc"
"overload jsdoc example"

🕗 Version & Regression Information

I've only just tried using this feature. Specifically based on the recent merged PR #51234 which is intended to fix #25590

⏯ Playground Link

Playground link with relevant code

screenshot of playground

💻 Code

//@ts-check

/*
I want following overloads for contructor
(a and b are differnt types):

new Foo()
new Foo(a)
new Foo(b)
new Foo(a,b)

I want to define overloads using JSDoc, ideally,
but would settle with a d.ts file. I can't find
any coherent examples other than the one in merged
PR, which does not seem to work for constructors.
*/

class Foo {
    #a
    #b

    /**
     * @overload
     * @constructor
     * @param {string} a
     * @param {number} b
     *//*
     * @overload
     * @constructor
     * @param {number} a
     *//*
     * @overload
     * @constructor
     * @param {string} a
     *//*
     * @constructor
     */
    constructor(a, b) {
        this.#a = a
        this.#b = b
    }
}

var a = new Foo()
var b = new Foo('str')
var c = new Foo(2)
var d = new Foo('str', 2)

🙁 Actual behavior

Code hints are not indicating the presence of overloads, and type checking does not seem to be applied correctly based on parameter combination.

🙂 Expected behavior

While instantiating the class I would expect to see the "up/down" arrows in the code hint that indicates presence of overloads (and allows cycling through the hints for the various overloads). I would also expect type checking to be applied properly.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 2, 2023
@sandersn sandersn changed the title JSDoc overload syntax introduced in recent PR does not appear to work @overload doesn't work for constructors Feb 2, 2023
@sandersn sandersn added the Bug A bug in TypeScript label Feb 2, 2023
@sandersn sandersn added this to the TypeScript 5.0.1 milestone Feb 2, 2023
@sandersn
Copy link
Member

sandersn commented Feb 2, 2023

Initial notes:

  1. @constructor is unexpected in the middle of an overload, and is only useful to the checker for marking constructor functions.
  2. All the but first comments start with /* instead of /**.

I'm trying to get it work after removing @constructor and adding asterisks.

@sandersn
Copy link
Member

sandersn commented Feb 2, 2023

With the syntax corrected like so

class Foo {
    #a
    #b

    /**
     * @constructor
     * @overload
     * @param {string} a
     * @param {number} b
     *//**
     * @constructor
     * @overload
     * @param {number} a
     *//**
     * @constructor
     * @overload
     * @param {string} a
     *//**
     * @constructor
     */
    constructor(a, b) {
        this.#a = a
        this.#b = b
    }
}

Quick info starts working correctly. Type checking is still broken -- it looks like resolveCall doesn't recognise the overloads when they're for a construct signature.

@sandersn
Copy link
Member

sandersn commented Feb 2, 2023

Yep, neither getReturnTypeOfSignature nor checkCallExpression recognised @overload and were returning any + an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants