Skip to content

Weird interaction between default arguments and strict-null #11424

Closed
@evmar

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...?)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions