microsoft / microsoft/TypeScript

?? and ??= behave differently when they shouldn't

Open
#40,359 10 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Control Flow Effort: Moderate Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.0.2, Nightly (2020-9-2)

Search Terms:
Nullish coalescing operator
Nullish never
Coalescing never
Nullish coalescing assignment

Code
According to this post usage of ?? and ??= should be equivalent in the sample code below:

const values: { a?: string[] } = { };

delete values.a;
(values.a ?? (values.a = [])).push("hello"); // error
values.a = undefined;
(values.a ?? (values.a = [])).push("hello"); // error

delete values.a;
(values.a ??= []).push("hello");
values.a = undefined;
(values.a ??= []).push("hello");

Expected behavior:
Examples with ?? and ??= should compile and behave similarly.

Actual behavior:
Examples with ?? do not compile (Argument of type 'string' is not assignable to parameter of type 'never'.(2345))

Playground Link:
link

Related Issues:
Didn't find anything similar

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied TypeScript example in the linked Playground and compare how ?? and ??= are checked for the optional string-array property. Done means the ?? expressions compile and behave consistently with the ??= examples, with coverage for both deleted and explicitly undefined properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.