Skip to content

Weird interaction between default arguments and strict-null #11424

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
evmar opened this issue Oct 6, 2016 · 2 comments
Closed

Weird interaction between default arguments and strict-null #11424

evmar opened this issue Oct 6, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@evmar
Copy link
Contributor

evmar commented Oct 6, 2016

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

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 6, 2016
@RyanCavanaugh
Copy link
Member

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

@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 7, 2016
@sandersn
Copy link
Member

sandersn commented Dec 8, 2016

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

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

4 participants