Skip to content

Default parameters should subtract undefined from internal function signature #12726

Closed
@blakeembrey

Description

@blakeembrey

TypeScript Version: 2.1.4

Code

function f (offset: number) {}

let x = (offset = 0) => {
  f(offset)
}

x = function (offset = 0) {
  f(offset)
}
{
  "compilerOptions": {
    "target": "es5",
    "outDir": "dist",
    "module": "commonjs",
    "noImplicitAny": true,
    "strictNullChecks": true
  }
}

Expected behavior: No error.

Actual behavior:

index.ts(8,5): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
  Type 'undefined' is not assignable to type 'number'.

I believe this has actually been an issue in previous releases, but 2.1 has made it apparent in other argument positions. I just tested 2.0 and can reproduce the above sample, but it doesn't fail on 2.0 with (x: string, y = 0) whereas 2.1 does.

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