Skip to content

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

Closed

Description

@luisabreu

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

Activity

  1. mhegazy commented on Oct 27, 2016

    @mhegazy
    Contributor

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

  2. added
    Working as Intended
    on Oct 27, 2016
  3. luisabreu commented on Oct 28, 2016

    @luisabreu
    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.

  4. mhegazy commented on Oct 28, 2016

    @mhegazy
    Contributor

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

  5. RyanCavanaugh commented on Oct 28, 2016

    @RyanCavanaugh
    Member

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

  6. mhegazy commented on Oct 28, 2016

    @mhegazy
    Contributor

    is --strictNullChecks on?

  7. added
    Bug
    and removed
    Working as Intended
    on Oct 28, 2016
  8. added this to the milestone on Oct 28, 2016
  9. mhegazy commented on Oct 28, 2016

    @mhegazy
    Contributor

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

  10. luisabreu commented on Oct 29, 2016

    @luisabreu
    Author

    Yep, strict null check is on.

  11. 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[/+] on Nov 3, 2016
  12. sandersn commented on Nov 3, 2016

    @sandersn
    Member

    Fix is up at #12033

  13. modified the milestones: TypeScript 2.2, on Dec 7, 2016
  14. akarzazi commented on Dec 7, 2016

    @akarzazi
    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 ?

  15. blakeembrey commented on Dec 7, 2016

    @blakeembrey
    Contributor

    Adel KARZAZI (@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).

  16. added
    Fixed
    on Feb 7, 2017
  17. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFixed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions