Skip to content

Include Default Parameter Values in Signature Help #16665

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

Open
mjbvz opened this issue Jun 21, 2017 · 43 comments
Open

Include Default Parameter Values in Signature Help #16665

mjbvz opened this issue Jun 21, 2017 · 43 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Domain: Signature Help Information in editor tooltips when invoking a function call Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Jun 21, 2017

From microsoft/vscode#28925

TypeScript Version: 2.4.0

Feature Request
signatureHelp current shows when a parameter is optional but does not include any information about that parameter's default value:

function foo(x = 10) { }
foo(|)

Result of TSServer signatureHelp on foo

[Trace  - 5:42:22 PM] Response received: signatureHelp (378). Request took 20 ms. Success: true 
Result: {
    "items": [
        {
            "isVariadic": false,
            "prefixDisplayParts": [
                {
                    "text": "foo",
                    "kind": "functionName"
                },
                {
                    "text": "(",
                    "kind": "punctuation"
                }
            ],
            "suffixDisplayParts": [
                {
                    "text": ")",
                    "kind": "punctuation"
                },
                {
                    "text": ":",
                    "kind": "punctuation"
                },
                {
                    "text": " ",
                    "kind": "space"
                },
                {
                    "text": "void",
                    "kind": "keyword"
                }
            ],
            "separatorDisplayParts": [
                {
                    "text": ",",
                    "kind": "punctuation"
                },
                {
                    "text": " ",
                    "kind": "space"
                }
            ],
            "parameters": [
                {
                    "name": "x",
                    "documentation": [],
                    "displayParts": [
                        {
                            "text": "x",
                            "kind": "parameterName"
                        },
                        {
                            "text": "?",
                            "kind": "punctuation"
                        },
                        {
                            "text": ":",
                            "kind": "punctuation"
                        },
                        {
                            "text": " ",
                            "kind": "space"
                        },
                        {
                            "text": "number",
                            "kind": "keyword"
                        }
                    ],
                    "isOptional": true
                }
            ],
            "documentation": [],
            "tags": []
        }
    ],
    "applicableSpan": {
        "start": {
            "line": 3,
            "offset": 5
        },
        "end": {
            "line": 3,
            "offset": 5
        }
    },
    "selectedItemIndex": 0,
    "argumentIndex": 0,
    "argumentCount": 0
}

When the default value is a simple literal type, it would be helpful to display this default value in the signature help. This information could be included in the displayParts response

@mjbvz mjbvz added the VS Code Tracked There is a VS Code equivalent to this issue label Jun 21, 2017
@sustained
Copy link

👍

@mhegazy mhegazy added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Aug 29, 2017
@billti
Copy link
Member

billti commented Aug 8, 2018

See feedback request here for also showing this information via the JsDoc comments: https://developercommunity.visualstudio.com/content/problem/230423/jsdoc.html

@edwinclement08
Copy link

Any Update on this?

@Yehonal
Copy link

Yehonal commented Dec 3, 2019

Updates?

@ivoiv
Copy link

ivoiv commented Dec 18, 2019

Also interested in updates.

@qiulang
Copy link

qiulang commented Feb 28, 2020

Hi #27442 was closed but how do I show default value ? I tried ctrl+shift+space on optional parameter but it did bot show the default value at all.

@relsqui
Copy link

relsqui commented Feb 28, 2020

@qiulang it's not implemented. that bug was closed because it's a duplicate of this one, but this one is still open and unaddressed.

@Zeal0us
Copy link

Zeal0us commented Mar 10, 2020

This would be great. It's something you kind of just expect to be there, and could cause someone not familiar to assume that the default is undefined, which may be confusing in certain contexts.

@alystair
Copy link

Also applicable to JS, not just TS

@amitsainii
Copy link

Any Updates on this?

@alystair
Copy link

Make sure you give a 👍 reaction to the issue - I feel that would help it get recognized more than simply bumping with comments

@patriciavandermeer
Copy link

this is really frustrating as the work around are all pretty hacky and make the jsdocs less helpful in other ways :(

@negebauer
Copy link

Would like to have this for jsdocs also

@delmarwichnieski
Copy link

for JSDoc JavaScript IntelliSense too

@mjbvz mjbvz added Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Domain: Signature Help Information in editor tooltips when invoking a function call labels Sep 28, 2020
@yoshhiii
Copy link

any update on this?

@csandman
Copy link

csandman commented Jun 2, 2021

Not sure if there has been any activity on this but I am also quite interested in this. I've been forced to add the default as a part of my description for each variable.

@davidmwhynot
Copy link

See feedback request here for also showing this information via the JsDoc comments: https://developercommunity.visualstudio.com/content/problem/230423/jsdoc.html

This was marked as low priority. Why is this the case?

@a-tarasyuk
Copy link
Contributor

@mjbvz What is the expected response from TSServer in this case?

  "parameters": [
    {
      "name": "x",
      "documentation": [],
      "displayParts": [
        {
          "text": "x",
          "kind": "parameterName"
        },
        {
          "text": "?",
          "kind": "punctuation"
        },
        {
          "text": ":",
          "kind": "punctuation"
        },
        {
          "text": " ",
          "kind": "space"
        },
        {
          "text": "number",
          "kind": "keyword"
        }
        ???
      ],
      "isOptional": true
    }
  ]

@mjbvz
Copy link
Contributor Author

mjbvz commented Aug 30, 2021

@a-tarasyuk I think something like:

     ....,
     {
          "text": " ",
          "kind": "space"
        },
        {
          "text": "=",
          "kind": "punctuation"
        },
       {
          "text": " ",
          "kind": "space"
        },
        {
          "text": "123",
          "kind": "stringLiteral"
        },

But double check with someone on the TS team for that

@a-tarasyuk
Copy link
Contributor

@mjbvz Thanks. @DanielRosenwasser Could you validate the response example?

@DanielRosenwasser
Copy link
Member

That looks correct to me (though to be pedantic, 123 wouldn't be a stringLiteral without quote marks around it).

What exactly do we want to do when the expression is huge though? Especially when both the type and the expression are big?

@bestknighter
Copy link

Any updates?

@a-tarasyuk
Copy link
Contributor

a-tarasyuk commented Nov 14, 2021

@DanielRosenwasser Do you mean something like this?

function fn(a = {
  b: () => {},
  c: 2,
  d: {
    e: [1, 3, 4]
  }
}) {}

Not sure what response should be in the above case. Maybe we only need to allow this for primitives or return the entire definition, I'm just not sure what kind should be in this case.

@mjbvz What do you think about a more complex case?

@tjx666
Copy link

tjx666 commented Dec 11, 2021

image

No default value support.

image

@alystair
Copy link

alystair commented Dec 31, 2021

@RyanCavanaugh @mjbvz this was created in 2017, how much longer must it remain in "Awaiting More Feedback" limbo? This is important information to provide users without (or no access to) external documentation.

@DavidBradbury
Copy link

I understand the desire to make this feature 'smart', but I think over-engineering is delaying this from getting done. In the JSDoc spec, it suggests a few examples and all you have to do is show what is literally defined in the optional parameter as text, not interpret arguments as given by the function.

Or if it is desired to show default values based on the function itself, and not JSDoc comment, this should be split into two issues instead of merging all the JSDoc related issues into this issue.

@alystair
Copy link

alystair commented Jan 6, 2022

In my mind it'd display the default values based on the function itself, which can be replaced if there's a superseding JSDoc comment.

@cox-michael
Copy link

Bump. This issue has been around for a long time.

@lewxdev
Copy link

lewxdev commented Nov 30, 2022

Not sure if this had been addressed previously, but I've found a workaround using type parameters with JSDoc using the @template tag which is arguably more powerful than simply just default parameters.
More details can be found via the JSDoc reference on the TypeScript documentation

Fix for @tjx666's snippet:

/**
 * 对数字取指定位数的小数
 * @template {number} [X=3]
 * @param {number} num
 * @param {X=} fractionDigits
 */
function toFixed(num, fractionDigits) {
    var numStr = num.toString(10);
    var dotIndex = numStr.indexOf('.');
    if (dotIndex === -1) return num;

    if (fractionDigits === undefined) fractionDigits = 3
    var multiplex = Math.pow(10, fractionDigits);
    return Math.round(num * multiplex) / multiplex;
}

Fix for the original example:

/**
 * @template {number} [T=10]
 * @param {T} x
 */
function foo(x = 10) { }

There does appear to be some discrepancy between the JSDoc implementation and the TypeScript interpreter, as the fix is roughly equivalent to the invalid code:

function foo<T extends number = 10>(x: T = 10) { }
// Type 'number' is not assignable to type 'T'.
//  'number' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'number'.ts(2322)

@i7eo
Copy link

i7eo commented Jan 4, 2023

same issue😢

@jamesfancy
Copy link

This is a incorrect type: type F1 = (a: number = 7) => void.
But F2 is correct: type F2 = (a?: number) => void;.
However F2 can be implements with a default parameter value such as

type F2 = (a?: number) => void;
const func2: F2 = (x: number = 7) => { };

If we try to get the type of (x: number = 7) => { }, that is (a?: number) => void:

const func3 = (x: number = 7) => { };
type F3 = typeof func3;  // (a?: number) => void

Because default parameter value is not a legal type rule.

How about make it legal?
Maybe the rule can be more detailed. for example:

type F1 = (a: number = 7) => void; // , declare default parameter with the special value.
const f11: F1 = (a: number = 7) => { } ;    // ✓, matchs exactly
const f12: F1 = (a?: number) => { };     // ✗  , expect default parameter value
const f13: F1 = (a?: number = 6) => { };  // ✗ , expect the special default parameter value (7)

type F2 = (a?: number) => void;
const f21: F2 = f11;   // ✓, f21() can run expectedly

Thus, if we use F1, we know it's default parameter value is always 7.

@andreisaikouski

This comment has been minimized.

@TheDusty01

This comment has been minimized.

@geksonES

This comment was marked as off-topic.

@JetUni

This comment has been minimized.

@fantasyz
Copy link

I hope I can see this implemented before I retire. If not I may not be able to die in peace.

@toridoriv
Copy link

I was looking for a solution or workaround for this and when I tried to react to a comment I realized that I've already done it... Two years ago 😩

@Josh-Grafman
Copy link

How is this not already a thing? Updates please.

@Diogo-Rossi
Copy link

I also want this. For JavaScript docs

Today I have to put this information in the parameter description

Using pure JavaScript:

/**
 * Sum 2 numbers
 * @param {Number} a First number
 * @param {Number} [b=0] Second number, defaults to 0.
 * @returns The sum
 */
function foo(a, b){
    if (b === undefined) b = 0;
    return a + b
}

image

@evtimDev
Copy link

need for javascript as well (not only ts).

@alystair
Copy link

alystair commented Jan 6, 2024

It's been 7 years at this point since the original request for a default values to appear in IDE popups. Every time I've brought this up in VSC they said it's a TypeScript repo issue - which feels ridiculous so many years later. I hope someone like @sandersn can raise this internally so it could be re-prioritized, as many programmers do not review the code of the function they are using, just the popup documentation, which can lead to unusual consequences if they are not made aware of default values.

microsoft/vscode#177529
#27442
microsoft/vscode#59609

@frostius
Copy link

This is the only thing holding me our team our company the world back from subscribing to copilot! Don't delay, elevate its priority today!

avcat added a commit to avcat/custom-select that referenced this issue Aug 28, 2024
Updated JSDoc for default values. Default values are not shown: microsoft/TypeScript#16665.
@FrancoisMentec

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Domain: Signature Help Information in editor tooltips when invoking a function call Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.