Skip to content

strictNullChecks and nullable parameters with defaults #14236

@jaredru

Description

@jaredru

TypeScript Version: 2.2.1

It seems that TS 2.2.1 changed the behavior of nullable parameters (NOTE: null specifically, not undefined) with defaults when compiling with --strictNullChecks. Was this change intentional? It's a breaking change.

I imagine this is a consequence of #12033, but AFAICT affecting null was likely an unintentional side effect.

Code

type OptionalString = string | null | undefined;

function hasDefault(val: OptionalString = "") {
  // TS 2.1.6, type of val is OptionalString
  // TS 2.2.1, type of val is string
}

// TS 2.1.6, next line compiled fine
// TS 2.2.1, error TS2345: Argument of type 'null' is not assignable to parameter of type 'string | undefined'.
hasDefault(null);

Expected behavior:
Type of val to be OptionalString (or string | null) and for null to be an accepted parameter.

Actual behavior:
Type of val is string and null is not accepted as an argument to the function.

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