Skip to content

Nullish coalescing should always include the type of the right operand #36393

@twavv

Description

@twavv
$ ./node_modules/.bin/tsc --version
Version 3.7.2

Search Terms:

Code

A toy example would be something like this.

let foo: string = "";
foo = "bar" ?? 123;

This one is obviously fine since "bar" is always truthy.

However, this becomes a little bit problematic when you consider the idiom of having Record objects and checking their truthiness before using them.

const elts = ["foo", "bar", "spam", "spam", "foo", "eggs"];
const counts: Record<string, number>;
for (const elt of elts) {
  // This really **should** raise an error.
  counts[elt] = counts[elt] ?? "zero";
  counts[elt] += 1;
}

Expected behavior:
An error should be raised.

Actual behavior:
Curiously, an error is not raised in strict mode but is raised in un-strict mode.

$ ./node_modules/.bin/tsc ./foo.ts 
foo.ts:5:3 - error TS2322: Type 'number | "zero"' is not assignable to type 'number'.
  Type '"zero"' is not assignable to type 'number'.

5   counts[elt] = counts[elt] ?? "zero";
    ~~~~~~~~~~~


Found 1 error.
$ ./node_modules/.bin/tsc --strict ./foo.ts
# No error, exits 0 and emits JS.

Playground Link:
Playground Link

Toggling the strictNullChecks config option will show the issue.

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions