Skip to content

Wrong function type when mixing default initialized parameters with regular parameters #11900

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
luisabreu opened this issue Oct 27, 2016 · 10 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@luisabreu
Copy link

VS Code does not show the correct parameter types on the tooltip when you mix default initialized parameters with regular parameters. Ex.:

function test(name= "John", age: number){}

The tooltip shown when you put the mouse cursor over test says (name: string, age: number): void instead of (name?:string, age: number).

wrongsignature

@mhegazy
Copy link
Contributor

mhegazy commented Oct 27, 2016

Optional parameters can not appear before required ones. so nome it is not optional. it just has a default parameter.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 27, 2016
@luisabreu
Copy link
Author

in my example, name is not an optional parameter. It's a default initialized parameter. According to the docs:

"Unlike plain optional parameters, default-initialized parameters don’t need to occur after required parameters. If a default-initialized parameter comes before a required parameter, users need to explicitly pass undefined to get the default initialized value. "

https://www.typescriptlang.org/docs/handbook/functions.html

The problem is that the tooltip is showing the wrong information, though the control flow is being performed correctly.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 28, 2016

Defaults do not show in signature anywhere else, they are considered function implementation detail.;so I am not sure I understand the request here.

@RyanCavanaugh
Copy link
Member

@mhegazy I think when a default exists, we should be pushing | undefined into the parameter type

@mhegazy
Copy link
Contributor

mhegazy commented Oct 28, 2016

is --strictNullChecks on?

@mhegazy mhegazy added Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Oct 28, 2016
@mhegazy mhegazy added this to the Future milestone Oct 28, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Oct 28, 2016

With --strictNullCheck yes it should be T | undefined.

@luisabreu
Copy link
Author

Yep, strict null check is on.

@sandersn sandersn changed the title Wrong function signature in VS Code when mixing default initialized parameters with regular parameters Wrong function type when mixing default initialized parameters with regular parameters Nov 3, 2016
@sandersn
Copy link
Member

sandersn commented Nov 3, 2016

Fix is up at #12033

@akarzazi
Copy link

akarzazi commented Dec 7, 2016

function AddProduct(product: string, quantity = 1) {
  // I think quantity should be number;
}

If default initialized parameter is automatically converted to T | undefined.
How can you enforce T as type ?

@blakeembrey
Copy link
Contributor

blakeembrey commented Dec 7, 2016

@akarzazi Inside the function would still be T, while the call signature is T | undefined as it has a default initialiser (the default value is used when the argument is undefined).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants