Skip to content

Weird interaction between default arguments and strict-null #11424

Closed

Description

@evmar

TypeScript Version: Version 2.0.5

Code

(with --strictNullChecks on)

function x(y = 'a', z: number) {}
function x1(y: string, z: number) {}

// Construct error messages to reveal the type of these functions:
let test1: void = x;
let test2: void = x1;
// They have the same type:
// ERROR: Type '(y: string, z: number) => void' is not assignable to type 'void'.
// ERROR: Type '(y: string, z: number) => void' is not assignable to type 'void'.

// However, they have different observable behaviors:
x(undefined, 1);  // OK
x1(undefined, 1);  // ERROR
// ERROR: Argument of type 'undefined' is not assignable to parameter of type 'string'.

Note that the second parameter (z) is necessary to trigger this behavior; if you drop z from this example, then the first param x has a ? as you'd expect.

(So it's even possible this is just a bug in how the function type is printed...?)

Activity

  1. RyanCavanaugh commented on Oct 6, 2016

    @RyanCavanaugh
    Member

    We should be displaying the defaulted parameter as x? or : string | undefined

  2. sandersn commented on Dec 8, 2016

    @sandersn
    Member

    This is a duplicate of #11900 that points out a different symptom of the bug that needs to be fixed.

  3. added
    Fixed
    on Feb 13, 2017
  4. modified the milestones: , TypeScript 2.3, TypeScript 2.2.1 on Feb 13, 2017
  5. 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