Microsoft/TypeScript

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

Open

#40,359 建立於 2020年9月2日

在 GitHub 查看
 (10 留言) (7 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: check: Control FlowEffort: ModerateHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

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

貢獻者指南