From 43dc59f26963ff1dff62a276790086dd507fec89 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:36:24 +0000 Subject: [PATCH 1/5] Initial plan From f1b5785cfb2e767b27084c31205f62a545411c78 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:28:40 +0000 Subject: [PATCH 2/5] Add flow path for falsy, non-nullish left operand of ?? and ??= Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> --- tsc/internal/ast/flow.go | 1 + tsc/internal/binder/binder.go | 35 +++++ tsc/internal/checker/flow.go | 7 +- .../controlFlowNullishCoalescingFalsyLeft.ts | 133 ++++++++++++++++++ 4 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts diff --git a/tsc/internal/ast/flow.go b/tsc/internal/ast/flow.go index 43a4bc2f0dfea..f56689cfd548a 100644 --- a/tsc/internal/ast/flow.go +++ b/tsc/internal/ast/flow.go @@ -20,6 +20,7 @@ const ( FlowFlagsReduceLabel FlowFlags = 1 << 10 // Temporarily reduce antecedents of label FlowFlagsReferenced FlowFlags = 1 << 11 // Referenced as antecedent once FlowFlagsShared FlowFlags = 1 << 12 // Referenced as antecedent more than once + FlowFlagsNonNullish FlowFlags = 1 << 13 // With FalseCondition, falsy but not nullish left operand of ?? or ??= FlowFlagsLabel = FlowFlagsBranchLabel | FlowFlagsLoopLabel FlowFlagsCondition = FlowFlagsTrueCondition | FlowFlagsFalseCondition ) diff --git a/tsc/internal/binder/binder.go b/tsc/internal/binder/binder.go index e1058bc33b043..f04df69fbc756 100644 --- a/tsc/internal/binder/binder.go +++ b/tsc/internal/binder/binder.go @@ -62,6 +62,8 @@ type Binder struct { currentReturnTarget *ast.FlowLabel currentTrueTarget *ast.FlowLabel currentFalseTarget *ast.FlowLabel + currentNullishTarget *ast.FlowLabel // Target of a nullish left operand of the ?? or ??= whose left operand is being bound + currentFalsyTarget *ast.FlowLabel // Target of a falsy, non-nullish left operand of that ?? or ??= currentExceptionTarget *ast.FlowLabel preSwitchCaseFlow *ast.FlowNode activeLabelList *ActiveLabel @@ -488,6 +490,9 @@ func (b *Binder) createFlowCondition(flags ast.FlowFlags, antecedent *ast.FlowNo } return b.unreachableFlow } + if flags&ast.FlowFlagsNonNullish != 0 && (expression.Kind == ast.KindTrueKeyword || expression.Kind == ast.KindNullKeyword) { + return b.unreachableFlow + } if (expression.Kind == ast.KindTrueKeyword && flags&ast.FlowFlagsFalseCondition != 0 || expression.Kind == ast.KindFalseKeyword && flags&ast.FlowFlagsTrueCondition != 0) && !ast.IsExpressionOfOptionalChainRoot(expression) && !ast.IsNullishCoalesce(expression.Parent) { return b.unreachableFlow } @@ -2301,6 +2306,8 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL preRightLabel := b.createBranchLabel() if expr.OperatorToken.Kind == ast.KindAmpersandAmpersandToken || expr.OperatorToken.Kind == ast.KindAmpersandAmpersandEqualsToken { b.bindCondition(expr.Left, preRightLabel, falseTarget) + } else if expr.OperatorToken.Kind == ast.KindQuestionQuestionToken || expr.OperatorToken.Kind == ast.KindQuestionQuestionEqualsToken { + b.bindNullishCoalescingLeft(node, trueTarget, falseTarget, preRightLabel) } else { b.bindCondition(expr.Left, trueTarget, preRightLabel) } @@ -2316,6 +2323,34 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL } } +// A left operand of ?? or ??= that is falsy but not nullish makes the whole expression falsy without +// evaluating the right operand, so we add a path from such a left operand to the false target. +func (b *Binder) bindNullishCoalescingLeft(node *ast.Node, trueTarget *ast.FlowLabel, falseTarget *ast.FlowLabel, preRightLabel *ast.FlowLabel) { + left := node.AsBinaryExpression().Left + falsyTarget := falseTarget + if falseTarget == b.currentNullishTarget { + // In (a ?? b) ?? c, a falsy, non-nullish a makes the left operand of the outer ?? falsy and non-nullish + falsyTarget = b.currentFalsyTarget + } else if parent := ast.WalkUpParenthesizedExpressions(node.Parent); ast.IsOptionalChainRoot(parent) && ast.SkipParentheses(parent.Expression()) == node { + // In (a ?? b)?.c, a falsy, non-nullish a continues the chain + falsyTarget = trueTarget + } + saveNullishTarget := b.currentNullishTarget + saveFalsyTarget := b.currentFalsyTarget + b.currentNullishTarget = preRightLabel + b.currentFalsyTarget = falsyTarget + b.bindCondition(left, trueTarget, preRightLabel) + b.currentNullishTarget = saveNullishTarget + b.currentFalsyTarget = saveFalsyTarget + if isLogicalAssignmentExpression(left) || ast.IsLogicalExpression(left) { + // Conservatively assume that any falsy outcome of a logical left operand may be non-nullish + b.addAntecedent(falsyTarget, b.finishFlowLabel(preRightLabel)) + } else if falsyTarget != trueTarget { + // When the targets are the same, the true condition of a non-nullish left operand already applies + b.addAntecedent(falsyTarget, b.createFlowCondition(ast.FlowFlagsFalseCondition|ast.FlowFlagsNonNullish, b.currentFlow, left)) + } +} + func (b *Binder) bindDeleteExpressionFlow(node *ast.Node) { expr := node.AsDeleteExpression() b.bindEachChild(node) diff --git a/tsc/internal/checker/flow.go b/tsc/internal/checker/flow.go index 05daef984819d..6128a369381ef 100644 --- a/tsc/internal/checker/flow.go +++ b/tsc/internal/checker/flow.go @@ -365,7 +365,12 @@ func (c *Checker) getTypeAtFlowCondition(f *FlowState, flow *ast.FlowNode) FlowT // *only* place a silent never type is ever generated. assumeTrue := flow.Flags&ast.FlowFlagsTrueCondition != 0 nonEvolvingType := c.finalizeEvolvingArrayType(flowType.t) - narrowedType := c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue) + var narrowedType *Type + if flow.Flags&ast.FlowFlagsNonNullish != 0 { + narrowedType = c.narrowTypeByTruthiness(f, c.narrowTypeByOptionality(f, nonEvolvingType, flow.Node, true /*assumePresent*/), flow.Node, false /*assumeTrue*/) + } else { + narrowedType = c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue) + } if narrowedType == nonEvolvingType { return flowType } diff --git a/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts b/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts new file mode 100644 index 0000000000000..27c62aa468a76 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts @@ -0,0 +1,133 @@ +// @strict: true +// @allowUnreachableCode: false +// @target: esnext + +declare function c(): boolean; +declare function d(): boolean | undefined; +declare function u(x: undefined): boolean; + +// https://github.com/microsoft/TypeScript/issues/65402 + +function f1() { + while (c() ?? (c() || true)) {} + c(); // Reachable +} + +function f2(a: "" | undefined, b: boolean) { + if (a ?? b) {} else { + const x: undefined = a; // Error + } +} + +// Narrowing of a falsy, non-nullish left operand + +function f3(a: boolean | undefined, b: boolean) { + if (a ?? b) { + a; // boolean | undefined + } + else { + a; // false | undefined + } +} + +function f4(a: string | number | undefined) { + if (a ?? (c() || true)) { + a; // string | number | undefined + } + else { + a; // string | number + } +} + +function f5(a: object | undefined, b: boolean) { + if (a ?? b) {} + else { + a; // undefined + const r: false = b; // Error, as with `if (a && b)` when `a: object` + } +} + +function f6(o: { p?: number }, b: boolean) { + if (o.p ?? b) {} + else { + o.p; // number | undefined + } +} + +function f7(a: "" | boolean | undefined, b: boolean) { + if (a ??= b) { + a; // "" | boolean + } + else { + a; // "" | false + } +} + +// Literal left operands + +function f8() { + while (true ?? (c() || true)) {} + c(); // Unreachable +} + +function f9() { + while (false ?? (c() || true)) {} + c(); // Reachable +} + +function f10() { + while (null ?? (c() || true)) {} + c(); // Unreachable +} + +// Logical left operands + +function f11() { + while ((c() || 0) ?? (c() || true)) {} + c(); // Reachable +} + +function f12() { + while (d() ?? d() ?? (c() || true)) {} + c(); // Reachable +} + +function f13(a: "" | undefined, b: "" | undefined) { + if (a ?? b ?? u(a)) { + a; // "" | undefined + } + else { + a; // "" | undefined + b; // "" | undefined + } + const r = a ?? b ?? u(a); +} + +function f14(x: boolean, z: 0 | undefined) { + let y: 0 | 1 | undefined; + (x || (y = z)) ?? (y = 1); + y; // 0 | 1 | undefined +} + +// Nested in logical expressions + +function f15(a: "" | undefined, b: true, d: boolean) { + if ((a ?? b) || d) {} + else { + a; // "" | undefined + } +} + +// Expressions of optional chains only test for nullishness + +function f16(a: "" | undefined) { + let b: 0 | 1 = 0; + if ((a ?? (b = 1, ""))?.[b = 1]) {} + else { + const one: 1 = b; + } +} + +function f17(a: "" | undefined, b: "" | undefined) { + (a ?? b ?? u(a))?.valueOf(); +} From 1ff6f2bf6d08895200a13e8331e288714de1ac35 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:05:55 +0000 Subject: [PATCH 3/5] Add missing nullish control-flow baselines and review regressions Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> --- ...lFlowNullishCoalescingFalsyLeft.errors.txt | 206 +++++++ .../controlFlowNullishCoalescingFalsyLeft.js | 295 ++++++++++ ...trolFlowNullishCoalescingFalsyLeft.symbols | 363 +++++++++++++ ...ontrolFlowNullishCoalescingFalsyLeft.types | 506 ++++++++++++++++++ .../controlFlowNullishCoalescingFalsyLeft.ts | 31 ++ 5 files changed, 1401 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js create mode 100644 tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt new file mode 100644 index 0000000000000..7451d28996a59 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt @@ -0,0 +1,206 @@ +controlFlowNullishCoalescingFalsyLeft.ts(14,15): error TS2322: Type '"" | undefined' is not assignable to type 'undefined'. + Type '""' is not assignable to type 'undefined'. +controlFlowNullishCoalescingFalsyLeft.ts(42,15): error TS2322: Type 'boolean' is not assignable to type 'false'. +controlFlowNullishCoalescingFalsyLeft.ts(65,12): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +controlFlowNullishCoalescingFalsyLeft.ts(66,5): error TS7027: Unreachable code detected. +controlFlowNullishCoalescingFalsyLeft.ts(70,12): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +controlFlowNullishCoalescingFalsyLeft.ts(75,12): error TS2871: This expression is always nullish. +controlFlowNullishCoalescingFalsyLeft.ts(76,5): error TS7027: Unreachable code detected. +controlFlowNullishCoalescingFalsyLeft.ts(134,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +controlFlowNullishCoalescingFalsyLeft.ts(139,14): error TS2871: This expression is always nullish. +controlFlowNullishCoalescingFalsyLeft.ts(144,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +controlFlowNullishCoalescingFalsyLeft.ts(153,11): error TS2322: Type '0 | 1' is not assignable to type '1'. + Type '0' is not assignable to type '1'. +controlFlowNullishCoalescingFalsyLeft.ts(159,11): error TS2322: Type '0 | 1' is not assignable to type '1'. + Type '0' is not assignable to type '1'. + + +==== controlFlowNullishCoalescingFalsyLeft.ts (12 errors) ==== + declare function c(): boolean; + declare function d(): boolean | undefined; + declare function u(x: undefined): boolean; + + // https://github.com/microsoft/TypeScript/issues/65402 + + function f1() { + while (c() ?? (c() || true)) {} + c(); // Reachable + } + + function f2(a: "" | undefined, b: boolean) { + if (a ?? b) {} else { + const x: undefined = a; // Error + ~ +!!! error TS2322: Type '"" | undefined' is not assignable to type 'undefined'. +!!! error TS2322: Type '""' is not assignable to type 'undefined'. + } + } + + // Narrowing of a falsy, non-nullish left operand + + function f3(a: boolean | undefined, b: boolean) { + if (a ?? b) { + a; // boolean | undefined + } + else { + a; // false | undefined + } + } + + function f4(a: string | number | undefined) { + if (a ?? (c() || true)) { + a; // string | number | undefined + } + else { + a; // string | number + } + } + + function f5(a: object | undefined, b: boolean) { + if (a ?? b) {} + else { + a; // undefined + const r: false = b; // Error, as with `if (a && b)` when `a: object` + ~ +!!! error TS2322: Type 'boolean' is not assignable to type 'false'. + } + } + + function f6(o: { p?: number }, b: boolean) { + if (o.p ?? b) {} + else { + o.p; // number | undefined + } + } + + function f7(a: "" | boolean | undefined, b: boolean) { + if (a ??= b) { + a; // "" | boolean + } + else { + a; // "" | false + } + } + + // Literal left operands + + function f8() { + while (true ?? (c() || true)) {} + ~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + c(); // Unreachable + ~~~~ +!!! error TS7027: Unreachable code detected. + } + + function f9() { + while (false ?? (c() || true)) {} + ~~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + c(); // Reachable + } + + function f10() { + while (null ?? (c() || true)) {} + ~~~~ +!!! error TS2871: This expression is always nullish. + c(); // Unreachable + ~~~~ +!!! error TS7027: Unreachable code detected. + } + + // Logical left operands + + function f11() { + while ((c() || 0) ?? (c() || true)) {} + c(); // Reachable + } + + function f12() { + while (d() ?? d() ?? (c() || true)) {} + c(); // Reachable + } + + function f13(a: "" | undefined, b: "" | undefined) { + if (a ?? b ?? u(a)) { + a; // "" | undefined + } + else { + a; // "" | undefined + b; // "" | undefined + } + const r = a ?? b ?? u(a); + } + + function f14(x: boolean, z: 0 | undefined) { + let y: 0 | 1 | undefined; + (x || (y = z)) ?? (y = 1); + y; // 0 | 1 | undefined + } + + // Nested in logical expressions + + function f15(a: "" | undefined, b: true, d: boolean) { + if ((a ?? b) || d) {} + else { + a; // "" | undefined + } + } + + // Expressions of optional chains only test for nullishness + + function f16(a: "" | undefined) { + let b: 0 | 1 = 0; + if ((a ?? (b = 1, ""))?.[b = 1]) {} + else { + const one: 1 = b; + } + } + + function f17(a: "" | undefined, b: "" | undefined) { + (a ?? b ?? u(a))?.valueOf(); + } + + // Parenthesized literal left operands + + function f18() { + while (((true)) ?? (c() || true)) {} + ~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + c(); // Unreachable + } + + function f19() { + while (((null)) ?? (c() || true)) {} + ~~~~ +!!! error TS2871: This expression is always nullish. + c(); // Unreachable + } + + function f20() { + while (((false)) ?? (c() || true)) {} + ~~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + c(); // Reachable + } + + // Optional-chain side effects still occur when nested in another ?? + + function f21(a: "" | undefined) { + let b: 0 | 1 = 0; + (a ?? (b = 1, ""))?.[b = 1] ?? false; + const one: 1 = b; + ~~~ +!!! error TS2322: Type '0 | 1' is not assignable to type '1'. +!!! error TS2322: Type '0' is not assignable to type '1'. + } + + function f22(a: "" | undefined) { + let b: 0 | 1 = 0; + (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; + const one: 1 = b; + ~~~ +!!! error TS2322: Type '0 | 1' is not assignable to type '1'. +!!! error TS2322: Type '0' is not assignable to type '1'. + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js new file mode 100644 index 0000000000000..55957eb18faf5 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js @@ -0,0 +1,295 @@ +//// [tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts] //// + +//// [controlFlowNullishCoalescingFalsyLeft.ts] +declare function c(): boolean; +declare function d(): boolean | undefined; +declare function u(x: undefined): boolean; + +// https://github.com/microsoft/TypeScript/issues/65402 + +function f1() { + while (c() ?? (c() || true)) {} + c(); // Reachable +} + +function f2(a: "" | undefined, b: boolean) { + if (a ?? b) {} else { + const x: undefined = a; // Error + } +} + +// Narrowing of a falsy, non-nullish left operand + +function f3(a: boolean | undefined, b: boolean) { + if (a ?? b) { + a; // boolean | undefined + } + else { + a; // false | undefined + } +} + +function f4(a: string | number | undefined) { + if (a ?? (c() || true)) { + a; // string | number | undefined + } + else { + a; // string | number + } +} + +function f5(a: object | undefined, b: boolean) { + if (a ?? b) {} + else { + a; // undefined + const r: false = b; // Error, as with `if (a && b)` when `a: object` + } +} + +function f6(o: { p?: number }, b: boolean) { + if (o.p ?? b) {} + else { + o.p; // number | undefined + } +} + +function f7(a: "" | boolean | undefined, b: boolean) { + if (a ??= b) { + a; // "" | boolean + } + else { + a; // "" | false + } +} + +// Literal left operands + +function f8() { + while (true ?? (c() || true)) {} + c(); // Unreachable +} + +function f9() { + while (false ?? (c() || true)) {} + c(); // Reachable +} + +function f10() { + while (null ?? (c() || true)) {} + c(); // Unreachable +} + +// Logical left operands + +function f11() { + while ((c() || 0) ?? (c() || true)) {} + c(); // Reachable +} + +function f12() { + while (d() ?? d() ?? (c() || true)) {} + c(); // Reachable +} + +function f13(a: "" | undefined, b: "" | undefined) { + if (a ?? b ?? u(a)) { + a; // "" | undefined + } + else { + a; // "" | undefined + b; // "" | undefined + } + const r = a ?? b ?? u(a); +} + +function f14(x: boolean, z: 0 | undefined) { + let y: 0 | 1 | undefined; + (x || (y = z)) ?? (y = 1); + y; // 0 | 1 | undefined +} + +// Nested in logical expressions + +function f15(a: "" | undefined, b: true, d: boolean) { + if ((a ?? b) || d) {} + else { + a; // "" | undefined + } +} + +// Expressions of optional chains only test for nullishness + +function f16(a: "" | undefined) { + let b: 0 | 1 = 0; + if ((a ?? (b = 1, ""))?.[b = 1]) {} + else { + const one: 1 = b; + } +} + +function f17(a: "" | undefined, b: "" | undefined) { + (a ?? b ?? u(a))?.valueOf(); +} + +// Parenthesized literal left operands + +function f18() { + while (((true)) ?? (c() || true)) {} + c(); // Unreachable +} + +function f19() { + while (((null)) ?? (c() || true)) {} + c(); // Unreachable +} + +function f20() { + while (((false)) ?? (c() || true)) {} + c(); // Reachable +} + +// Optional-chain side effects still occur when nested in another ?? + +function f21(a: "" | undefined) { + let b: 0 | 1 = 0; + (a ?? (b = 1, ""))?.[b = 1] ?? false; + const one: 1 = b; +} + +function f22(a: "" | undefined) { + let b: 0 | 1 = 0; + (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; + const one: 1 = b; +} + + +//// [controlFlowNullishCoalescingFalsyLeft.js] +"use strict"; +// https://github.com/microsoft/TypeScript/issues/65402 +function f1() { + while (c() ?? (c() || true)) { } + c(); // Reachable +} +function f2(a, b) { + if (a ?? b) { } + else { + const x = a; // Error + } +} +// Narrowing of a falsy, non-nullish left operand +function f3(a, b) { + if (a ?? b) { + a; // boolean | undefined + } + else { + a; // false | undefined + } +} +function f4(a) { + if (a ?? (c() || true)) { + a; // string | number | undefined + } + else { + a; // string | number + } +} +function f5(a, b) { + if (a ?? b) { } + else { + a; // undefined + const r = b; // Error, as with `if (a && b)` when `a: object` + } +} +function f6(o, b) { + if (o.p ?? b) { } + else { + o.p; // number | undefined + } +} +function f7(a, b) { + if (a ??= b) { + a; // "" | boolean + } + else { + a; // "" | false + } +} +// Literal left operands +function f8() { + while (true ?? (c() || true)) { } + c(); // Unreachable +} +function f9() { + while (false ?? (c() || true)) { } + c(); // Reachable +} +function f10() { + while (null ?? (c() || true)) { } + c(); // Unreachable +} +// Logical left operands +function f11() { + while ((c() || 0) ?? (c() || true)) { } + c(); // Reachable +} +function f12() { + while (d() ?? d() ?? (c() || true)) { } + c(); // Reachable +} +function f13(a, b) { + if (a ?? b ?? u(a)) { + a; // "" | undefined + } + else { + a; // "" | undefined + b; // "" | undefined + } + const r = a ?? b ?? u(a); +} +function f14(x, z) { + let y; + (x || (y = z)) ?? (y = 1); + y; // 0 | 1 | undefined +} +// Nested in logical expressions +function f15(a, b, d) { + if ((a ?? b) || d) { } + else { + a; // "" | undefined + } +} +// Expressions of optional chains only test for nullishness +function f16(a) { + let b = 0; + if ((a ?? (b = 1, ""))?.[b = 1]) { } + else { + const one = b; + } +} +function f17(a, b) { + (a ?? b ?? u(a))?.valueOf(); +} +// Parenthesized literal left operands +function f18() { + while (((true)) ?? (c() || true)) { } + c(); // Unreachable +} +function f19() { + while (((null)) ?? (c() || true)) { } + c(); // Unreachable +} +function f20() { + while (((false)) ?? (c() || true)) { } + c(); // Reachable +} +// Optional-chain side effects still occur when nested in another ?? +function f21(a) { + let b = 0; + (a ?? (b = 1, ""))?.[b = 1] ?? false; + const one = b; +} +function f22(a) { + let b = 0; + (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; + const one = b; +} diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols new file mode 100644 index 0000000000000..b349da91fdf91 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols @@ -0,0 +1,363 @@ +//// [tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts] //// + +=== controlFlowNullishCoalescingFalsyLeft.ts === +declare function c(): boolean; +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + +declare function d(): boolean | undefined; +>d : Symbol(d, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 30)) + +declare function u(x: undefined): boolean; +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 1, 42)) +>x : Symbol(x, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 2, 19)) + +// https://github.com/microsoft/TypeScript/issues/65402 + +function f1() { +>f1 : Symbol(f1, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 2, 42)) + + while (c() ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Reachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +function f2(a: "" | undefined, b: boolean) { +>f2 : Symbol(f2, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 9, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 11, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 11, 30)) + + if (a ?? b) {} else { +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 11, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 11, 30)) + + const x: undefined = a; // Error +>x : Symbol(x, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 13, 13)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 11, 12)) + } +} + +// Narrowing of a falsy, non-nullish left operand + +function f3(a: boolean | undefined, b: boolean) { +>f3 : Symbol(f3, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 15, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 19, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 19, 35)) + + if (a ?? b) { +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 19, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 19, 35)) + + a; // boolean | undefined +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 19, 12)) + } + else { + a; // false | undefined +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 19, 12)) + } +} + +function f4(a: string | number | undefined) { +>f4 : Symbol(f4, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 26, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 28, 12)) + + if (a ?? (c() || true)) { +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 28, 12)) +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + a; // string | number | undefined +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 28, 12)) + } + else { + a; // string | number +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 28, 12)) + } +} + +function f5(a: object | undefined, b: boolean) { +>f5 : Symbol(f5, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 35, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 37, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 37, 34)) + + if (a ?? b) {} +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 37, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 37, 34)) + + else { + a; // undefined +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 37, 12)) + + const r: false = b; // Error, as with `if (a && b)` when `a: object` +>r : Symbol(r, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 41, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 37, 34)) + } +} + +function f6(o: { p?: number }, b: boolean) { +>f6 : Symbol(f6, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 43, 1)) +>o : Symbol(o, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 12)) +>p : Symbol(p, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 16)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 30)) + + if (o.p ?? b) {} +>o.p : Symbol(p, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 16)) +>o : Symbol(o, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 12)) +>p : Symbol(p, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 16)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 30)) + + else { + o.p; // number | undefined +>o.p : Symbol(p, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 16)) +>o : Symbol(o, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 12)) +>p : Symbol(p, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 45, 16)) + } +} + +function f7(a: "" | boolean | undefined, b: boolean) { +>f7 : Symbol(f7, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 50, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 52, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 52, 40)) + + if (a ??= b) { +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 52, 12)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 52, 40)) + + a; // "" | boolean +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 52, 12)) + } + else { + a; // "" | false +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 52, 12)) + } +} + +// Literal left operands + +function f8() { +>f8 : Symbol(f8, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 59, 1)) + + while (true ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Unreachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +function f9() { +>f9 : Symbol(f9, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 66, 1)) + + while (false ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Reachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +function f10() { +>f10 : Symbol(f10, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 71, 1)) + + while (null ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Unreachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +// Logical left operands + +function f11() { +>f11 : Symbol(f11, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 76, 1)) + + while ((c() || 0) ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Reachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +function f12() { +>f12 : Symbol(f12, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 83, 1)) + + while (d() ?? d() ?? (c() || true)) {} +>d : Symbol(d, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 30)) +>d : Symbol(d, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 30)) +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Reachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +function f13(a: "" | undefined, b: "" | undefined) { +>f13 : Symbol(f13, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 88, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 31)) + + if (a ?? b ?? u(a)) { +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 31)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 1, 42)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 13)) + + a; // "" | undefined +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 13)) + } + else { + a; // "" | undefined +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 13)) + + b; // "" | undefined +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 31)) + } + const r = a ?? b ?? u(a); +>r : Symbol(r, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 98, 9)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 31)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 1, 42)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 90, 13)) +} + +function f14(x: boolean, z: 0 | undefined) { +>f14 : Symbol(f14, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 99, 1)) +>x : Symbol(x, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 101, 13)) +>z : Symbol(z, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 101, 24)) + + let y: 0 | 1 | undefined; +>y : Symbol(y, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 102, 7)) + + (x || (y = z)) ?? (y = 1); +>x : Symbol(x, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 101, 13)) +>y : Symbol(y, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 102, 7)) +>z : Symbol(z, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 101, 24)) +>y : Symbol(y, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 102, 7)) + + y; // 0 | 1 | undefined +>y : Symbol(y, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 102, 7)) +} + +// Nested in logical expressions + +function f15(a: "" | undefined, b: true, d: boolean) { +>f15 : Symbol(f15, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 105, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 109, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 109, 31)) +>d : Symbol(d, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 109, 40)) + + if ((a ?? b) || d) {} +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 109, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 109, 31)) +>d : Symbol(d, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 109, 40)) + + else { + a; // "" | undefined +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 109, 13)) + } +} + +// Expressions of optional chains only test for nullishness + +function f16(a: "" | undefined) { +>f16 : Symbol(f16, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 114, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 118, 13)) + + let b: 0 | 1 = 0; +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 119, 7)) + + if ((a ?? (b = 1, ""))?.[b = 1]) {} +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 118, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 119, 7)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 119, 7)) + + else { + const one: 1 = b; +>one : Symbol(one, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 122, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 119, 7)) + } +} + +function f17(a: "" | undefined, b: "" | undefined) { +>f17 : Symbol(f17, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 124, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 126, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 126, 31)) + + (a ?? b ?? u(a))?.valueOf(); +>(a ?? b ?? u(a))?.valueOf : Symbol(valueOf, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 126, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 126, 31)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 1, 42)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 126, 13)) +>valueOf : Symbol(valueOf, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +// Parenthesized literal left operands + +function f18() { +>f18 : Symbol(f18, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 128, 1)) + + while (((true)) ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Unreachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +function f19() { +>f19 : Symbol(f19, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 135, 1)) + + while (((null)) ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Unreachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +function f20() { +>f20 : Symbol(f20, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 140, 1)) + + while (((false)) ?? (c() || true)) {} +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) + + c(); // Reachable +>c : Symbol(c, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 0, 0)) +} + +// Optional-chain side effects still occur when nested in another ?? + +function f21(a: "" | undefined) { +>f21 : Symbol(f21, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 145, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 149, 13)) + + let b: 0 | 1 = 0; +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 150, 7)) + + (a ?? (b = 1, ""))?.[b = 1] ?? false; +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 149, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 150, 7)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 150, 7)) + + const one: 1 = b; +>one : Symbol(one, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 152, 9)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 150, 7)) +} + +function f22(a: "" | undefined) { +>f22 : Symbol(f22, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 153, 1)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 155, 13)) + + let b: 0 | 1 = 0; +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 156, 7)) + + (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; +>(a ?? (b = 1, ""))?.charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --)) +>a : Symbol(a, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 155, 13)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 156, 7)) +>charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 156, 7)) + + const one: 1 = b; +>one : Symbol(one, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 158, 9)) +>b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 156, 7)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types new file mode 100644 index 0000000000000..f0670b675919c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types @@ -0,0 +1,506 @@ +//// [tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts] //// + +=== controlFlowNullishCoalescingFalsyLeft.ts === +declare function c(): boolean; +>c : () => boolean + +declare function d(): boolean | undefined; +>d : () => boolean | undefined + +declare function u(x: undefined): boolean; +>u : (x: undefined) => boolean +>x : undefined + +// https://github.com/microsoft/TypeScript/issues/65402 + +function f1() { +>f1 : () => void + + while (c() ?? (c() || true)) {} +>c() ?? (c() || true) : boolean +>c() : boolean +>c : () => boolean +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Reachable +>c() : boolean +>c : () => boolean +} + +function f2(a: "" | undefined, b: boolean) { +>f2 : (a: "" | undefined, b: boolean) => void +>a : "" | undefined +>b : boolean + + if (a ?? b) {} else { +>a ?? b : "" | boolean +>a : "" | undefined +>b : boolean + + const x: undefined = a; // Error +>x : undefined +>a : "" | undefined + } +} + +// Narrowing of a falsy, non-nullish left operand + +function f3(a: boolean | undefined, b: boolean) { +>f3 : (a: boolean | undefined, b: boolean) => void +>a : boolean | undefined +>b : boolean + + if (a ?? b) { +>a ?? b : boolean +>a : boolean | undefined +>b : boolean + + a; // boolean | undefined +>a : boolean | undefined + } + else { + a; // false | undefined +>a : false | undefined + } +} + +function f4(a: string | number | undefined) { +>f4 : (a: string | number | undefined) => void +>a : string | number | undefined + + if (a ?? (c() || true)) { +>a ?? (c() || true) : string | number | true +>a : string | number | undefined +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + a; // string | number | undefined +>a : string | number | undefined + } + else { + a; // string | number +>a : string | number + } +} + +function f5(a: object | undefined, b: boolean) { +>f5 : (a: object | undefined, b: boolean) => void +>a : object | undefined +>b : boolean + + if (a ?? b) {} +>a ?? b : boolean | object +>a : object | undefined +>b : boolean + + else { + a; // undefined +>a : undefined + + const r: false = b; // Error, as with `if (a && b)` when `a: object` +>r : false +>false : false +>b : boolean + } +} + +function f6(o: { p?: number }, b: boolean) { +>f6 : (o: { p?: number; }, b: boolean) => void +>o : { p?: number; } +>p : number | undefined +>b : boolean + + if (o.p ?? b) {} +>o.p ?? b : number | boolean +>o.p : number | undefined +>o : { p?: number; } +>p : number | undefined +>b : boolean + + else { + o.p; // number | undefined +>o.p : number | undefined +>o : { p?: number; } +>p : number | undefined + } +} + +function f7(a: "" | boolean | undefined, b: boolean) { +>f7 : (a: "" | boolean | undefined, b: boolean) => void +>a : "" | boolean | undefined +>b : boolean + + if (a ??= b) { +>a ??= b : "" | boolean +>a : "" | boolean | undefined +>b : boolean + + a; // "" | boolean +>a : "" | boolean + } + else { + a; // "" | false +>a : "" | false + } +} + +// Literal left operands + +function f8() { +>f8 : () => void + + while (true ?? (c() || true)) {} +>true ?? (c() || true) : true +>true : true +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Unreachable +>c() : boolean +>c : () => boolean +} + +function f9() { +>f9 : () => void + + while (false ?? (c() || true)) {} +>false ?? (c() || true) : false +>false : false +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Reachable +>c() : boolean +>c : () => boolean +} + +function f10() { +>f10 : () => void + + while (null ?? (c() || true)) {} +>null ?? (c() || true) : true +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Unreachable +>c() : boolean +>c : () => boolean +} + +// Logical left operands + +function f11() { +>f11 : () => void + + while ((c() || 0) ?? (c() || true)) {} +>(c() || 0) ?? (c() || true) : 0 | true +>(c() || 0) : 0 | true +>c() || 0 : 0 | true +>c() : boolean +>c : () => boolean +>0 : 0 +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Reachable +>c() : boolean +>c : () => boolean +} + +function f12() { +>f12 : () => void + + while (d() ?? d() ?? (c() || true)) {} +>d() ?? d() ?? (c() || true) : boolean +>d() ?? d() : boolean | undefined +>d() : boolean | undefined +>d : () => boolean | undefined +>d() : boolean | undefined +>d : () => boolean | undefined +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Reachable +>c() : boolean +>c : () => boolean +} + +function f13(a: "" | undefined, b: "" | undefined) { +>f13 : (a: "" | undefined, b: "" | undefined) => void +>a : "" | undefined +>b : "" | undefined + + if (a ?? b ?? u(a)) { +>a ?? b ?? u(a) : "" | boolean +>a ?? b : "" | undefined +>a : "" | undefined +>b : "" | undefined +>u(a) : boolean +>u : (x: undefined) => boolean +>a : undefined + + a; // "" | undefined +>a : "" | undefined + } + else { + a; // "" | undefined +>a : "" | undefined + + b; // "" | undefined +>b : "" | undefined + } + const r = a ?? b ?? u(a); +>r : "" | boolean +>a ?? b ?? u(a) : "" | boolean +>a ?? b : "" | undefined +>a : "" | undefined +>b : "" | undefined +>u(a) : boolean +>u : (x: undefined) => boolean +>a : undefined +} + +function f14(x: boolean, z: 0 | undefined) { +>f14 : (x: boolean, z: 0 | undefined) => void +>x : boolean +>z : 0 | undefined + + let y: 0 | 1 | undefined; +>y : 0 | 1 | undefined + + (x || (y = z)) ?? (y = 1); +>(x || (y = z)) ?? (y = 1) : 0 | 1 | true +>(x || (y = z)) : 0 | true | undefined +>x || (y = z) : 0 | true | undefined +>x : boolean +>(y = z) : 0 | undefined +>y = z : 0 | undefined +>y : 0 | 1 | undefined +>z : 0 | undefined +>(y = 1) : 1 +>y = 1 : 1 +>y : 0 | 1 | undefined +>1 : 1 + + y; // 0 | 1 | undefined +>y : 0 | 1 | undefined +} + +// Nested in logical expressions + +function f15(a: "" | undefined, b: true, d: boolean) { +>f15 : (a: "" | undefined, b: true, d: boolean) => void +>a : "" | undefined +>b : true +>true : true +>d : boolean + + if ((a ?? b) || d) {} +>(a ?? b) || d : boolean +>(a ?? b) : "" | true +>a ?? b : "" | true +>a : "" | undefined +>b : true +>d : boolean + + else { + a; // "" | undefined +>a : "" | undefined + } +} + +// Expressions of optional chains only test for nullishness + +function f16(a: "" | undefined) { +>f16 : (a: "" | undefined) => void +>a : "" | undefined + + let b: 0 | 1 = 0; +>b : 0 | 1 +>0 : 0 + + if ((a ?? (b = 1, ""))?.[b = 1]) {} +>(a ?? (b = 1, ""))?.[b = 1] : string +>(a ?? (b = 1, "")) : "" +>a ?? (b = 1, "") : "" +>a : "" | undefined +>(b = 1, "") : "" +>b = 1, "" : "" +>b = 1 : 1 +>b : 0 | 1 +>1 : 1 +>"" : "" +>b = 1 : 1 +>b : 0 | 1 +>1 : 1 + + else { + const one: 1 = b; +>one : 1 +>b : 1 + } +} + +function f17(a: "" | undefined, b: "" | undefined) { +>f17 : (a: "" | undefined, b: "" | undefined) => void +>a : "" | undefined +>b : "" | undefined + + (a ?? b ?? u(a))?.valueOf(); +>(a ?? b ?? u(a))?.valueOf() : string | boolean +>(a ?? b ?? u(a))?.valueOf : (() => string) | (() => boolean) +>(a ?? b ?? u(a)) : "" | boolean +>a ?? b ?? u(a) : "" | boolean +>a ?? b : "" | undefined +>a : "" | undefined +>b : "" | undefined +>u(a) : boolean +>u : (x: undefined) => boolean +>a : undefined +>valueOf : (() => string) | (() => boolean) +} + +// Parenthesized literal left operands + +function f18() { +>f18 : () => void + + while (((true)) ?? (c() || true)) {} +>((true)) ?? (c() || true) : true +>((true)) : true +>(true) : true +>true : true +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Unreachable +>c() : boolean +>c : () => boolean +} + +function f19() { +>f19 : () => void + + while (((null)) ?? (c() || true)) {} +>((null)) ?? (c() || true) : true +>((null)) : null +>(null) : null +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Unreachable +>c() : boolean +>c : () => boolean +} + +function f20() { +>f20 : () => void + + while (((false)) ?? (c() || true)) {} +>((false)) ?? (c() || true) : false +>((false)) : false +>(false) : false +>false : false +>(c() || true) : true +>c() || true : true +>c() : boolean +>c : () => boolean +>true : true + + c(); // Reachable +>c() : boolean +>c : () => boolean +} + +// Optional-chain side effects still occur when nested in another ?? + +function f21(a: "" | undefined) { +>f21 : (a: "" | undefined) => void +>a : "" | undefined + + let b: 0 | 1 = 0; +>b : 0 | 1 +>0 : 0 + + (a ?? (b = 1, ""))?.[b = 1] ?? false; +>(a ?? (b = 1, ""))?.[b = 1] ?? false : string +>(a ?? (b = 1, ""))?.[b = 1] : string +>(a ?? (b = 1, "")) : "" +>a ?? (b = 1, "") : "" +>a : "" | undefined +>(b = 1, "") : "" +>b = 1, "" : "" +>b = 1 : 1 +>b : 0 | 1 +>1 : 1 +>"" : "" +>b = 1 : 1 +>b : 0 | 1 +>1 : 1 +>false : false + + const one: 1 = b; +>one : 1 +>b : 0 | 1 +} + +function f22(a: "" | undefined) { +>f22 : (a: "" | undefined) => void +>a : "" | undefined + + let b: 0 | 1 = 0; +>b : 0 | 1 +>0 : 0 + + (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; +>(a ?? (b = 1, ""))?.charAt(b = 1) ?? false : string +>(a ?? (b = 1, ""))?.charAt(b = 1) : string +>(a ?? (b = 1, ""))?.charAt : (pos: number) => string +>(a ?? (b = 1, "")) : "" +>a ?? (b = 1, "") : "" +>a : "" | undefined +>(b = 1, "") : "" +>b = 1, "" : "" +>b = 1 : 1 +>b : 0 | 1 +>1 : 1 +>"" : "" +>charAt : (pos: number) => string +>b = 1 : 1 +>b : 0 | 1 +>1 : 1 +>false : false + + const one: 1 = b; +>one : 1 +>b : 0 | 1 +} + diff --git a/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts b/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts index 27c62aa468a76..d0c7fe76675e1 100644 --- a/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts +++ b/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts @@ -131,3 +131,34 @@ function f16(a: "" | undefined) { function f17(a: "" | undefined, b: "" | undefined) { (a ?? b ?? u(a))?.valueOf(); } + +// Parenthesized literal left operands + +function f18() { + while (((true)) ?? (c() || true)) {} + c(); // Unreachable +} + +function f19() { + while (((null)) ?? (c() || true)) {} + c(); // Unreachable +} + +function f20() { + while (((false)) ?? (c() || true)) {} + c(); // Reachable +} + +// Optional-chain side effects still occur when nested in another ?? + +function f21(a: "" | undefined) { + let b: 0 | 1 = 0; + (a ?? (b = 1, ""))?.[b = 1] ?? false; + const one: 1 = b; +} + +function f22(a: "" | undefined) { + let b: 0 | 1 = 0; + (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; + const one: 1 = b; +} From f89b8bb80d227ae7be5f501d12fb590c786392e2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:13:00 +0000 Subject: [PATCH 4/5] Preserve literal reachability and optional-chain effects in nullish flow Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> --- tsc/internal/binder/binder.go | 15 +++++++++------ ...trolFlowNullishCoalescingFalsyLeft.errors.txt | 16 ++++++---------- .../controlFlowNullishCoalescingFalsyLeft.types | 4 ++-- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/tsc/internal/binder/binder.go b/tsc/internal/binder/binder.go index f04df69fbc756..83f9ea754d5e4 100644 --- a/tsc/internal/binder/binder.go +++ b/tsc/internal/binder/binder.go @@ -490,8 +490,11 @@ func (b *Binder) createFlowCondition(flags ast.FlowFlags, antecedent *ast.FlowNo } return b.unreachableFlow } - if flags&ast.FlowFlagsNonNullish != 0 && (expression.Kind == ast.KindTrueKeyword || expression.Kind == ast.KindNullKeyword) { - return b.unreachableFlow + if flags&ast.FlowFlagsNonNullish != 0 { + kind := ast.SkipParentheses(expression).Kind + if kind == ast.KindTrueKeyword || kind == ast.KindNullKeyword { + return b.unreachableFlow + } } if (expression.Kind == ast.KindTrueKeyword && flags&ast.FlowFlagsFalseCondition != 0 || expression.Kind == ast.KindFalseKeyword && flags&ast.FlowFlagsTrueCondition != 0) && !ast.IsExpressionOfOptionalChainRoot(expression) && !ast.IsNullishCoalesce(expression.Parent) { return b.unreachableFlow @@ -2328,12 +2331,12 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL func (b *Binder) bindNullishCoalescingLeft(node *ast.Node, trueTarget *ast.FlowLabel, falseTarget *ast.FlowLabel, preRightLabel *ast.FlowLabel) { left := node.AsBinaryExpression().Left falsyTarget := falseTarget - if falseTarget == b.currentNullishTarget { - // In (a ?? b) ?? c, a falsy, non-nullish a makes the left operand of the outer ?? falsy and non-nullish - falsyTarget = b.currentFalsyTarget - } else if parent := ast.WalkUpParenthesizedExpressions(node.Parent); ast.IsOptionalChainRoot(parent) && ast.SkipParentheses(parent.Expression()) == node { + if parent := ast.WalkUpParenthesizedExpressions(node.Parent); ast.IsOptionalChainRoot(parent) && ast.SkipParentheses(parent.Expression()) == node { // In (a ?? b)?.c, a falsy, non-nullish a continues the chain falsyTarget = trueTarget + } else if falseTarget == b.currentNullishTarget { + // In (a ?? b) ?? c, a falsy, non-nullish a makes the left operand of the outer ?? falsy and non-nullish + falsyTarget = b.currentFalsyTarget } saveNullishTarget := b.currentNullishTarget saveFalsyTarget := b.currentFalsyTarget diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt index 7451d28996a59..38523bc551b79 100644 --- a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt @@ -7,12 +7,10 @@ controlFlowNullishCoalescingFalsyLeft.ts(70,12): error TS2869: Right operand of controlFlowNullishCoalescingFalsyLeft.ts(75,12): error TS2871: This expression is always nullish. controlFlowNullishCoalescingFalsyLeft.ts(76,5): error TS7027: Unreachable code detected. controlFlowNullishCoalescingFalsyLeft.ts(134,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +controlFlowNullishCoalescingFalsyLeft.ts(135,5): error TS7027: Unreachable code detected. controlFlowNullishCoalescingFalsyLeft.ts(139,14): error TS2871: This expression is always nullish. +controlFlowNullishCoalescingFalsyLeft.ts(140,5): error TS7027: Unreachable code detected. controlFlowNullishCoalescingFalsyLeft.ts(144,14): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. -controlFlowNullishCoalescingFalsyLeft.ts(153,11): error TS2322: Type '0 | 1' is not assignable to type '1'. - Type '0' is not assignable to type '1'. -controlFlowNullishCoalescingFalsyLeft.ts(159,11): error TS2322: Type '0 | 1' is not assignable to type '1'. - Type '0' is not assignable to type '1'. ==== controlFlowNullishCoalescingFalsyLeft.ts (12 errors) ==== @@ -168,6 +166,8 @@ controlFlowNullishCoalescingFalsyLeft.ts(159,11): error TS2322: Type '0 | 1' is ~~~~ !!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. c(); // Unreachable + ~~~~ +!!! error TS7027: Unreachable code detected. } function f19() { @@ -175,6 +175,8 @@ controlFlowNullishCoalescingFalsyLeft.ts(159,11): error TS2322: Type '0 | 1' is ~~~~ !!! error TS2871: This expression is always nullish. c(); // Unreachable + ~~~~ +!!! error TS7027: Unreachable code detected. } function f20() { @@ -190,17 +192,11 @@ controlFlowNullishCoalescingFalsyLeft.ts(159,11): error TS2322: Type '0 | 1' is let b: 0 | 1 = 0; (a ?? (b = 1, ""))?.[b = 1] ?? false; const one: 1 = b; - ~~~ -!!! error TS2322: Type '0 | 1' is not assignable to type '1'. -!!! error TS2322: Type '0' is not assignable to type '1'. } function f22(a: "" | undefined) { let b: 0 | 1 = 0; (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; const one: 1 = b; - ~~~ -!!! error TS2322: Type '0 | 1' is not assignable to type '1'. -!!! error TS2322: Type '0' is not assignable to type '1'. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types index f0670b675919c..d86120050142b 100644 --- a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types @@ -469,7 +469,7 @@ function f21(a: "" | undefined) { const one: 1 = b; >one : 1 ->b : 0 | 1 +>b : 1 } function f22(a: "" | undefined) { @@ -501,6 +501,6 @@ function f22(a: "" | undefined) { const one: 1 = b; >one : 1 ->b : 0 | 1 +>b : 1 } From 3dce279f2b175944666a8d1109c06d3ea6a2dae5 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Tue, 10 Nov 2026 23:11:06 -0800 Subject: [PATCH 5/5] Normalize wrapped nullish-coalescing operands before narrowing Unwrap outer expressions before narrowing the falsy, non-nullish flow path. Cover parenthesized and non-null-asserted discriminant accesses and update the compiler baselines. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 34bc92cf-9741-463b-bcf9-2f654427cd4d --- tsc/internal/checker/flow.go | 3 +- ...lFlowNullishCoalescingFalsyLeft.errors.txt | 12 +++++ .../controlFlowNullishCoalescingFalsyLeft.js | 24 +++++++++ ...trolFlowNullishCoalescingFalsyLeft.symbols | 38 ++++++++++++++ ...ontrolFlowNullishCoalescingFalsyLeft.types | 50 +++++++++++++++++++ .../controlFlowNullishCoalescingFalsyLeft.ts | 12 +++++ 6 files changed, 138 insertions(+), 1 deletion(-) diff --git a/tsc/internal/checker/flow.go b/tsc/internal/checker/flow.go index 6128a369381ef..fa55283aeb735 100644 --- a/tsc/internal/checker/flow.go +++ b/tsc/internal/checker/flow.go @@ -367,7 +367,8 @@ func (c *Checker) getTypeAtFlowCondition(f *FlowState, flow *ast.FlowNode) FlowT nonEvolvingType := c.finalizeEvolvingArrayType(flowType.t) var narrowedType *Type if flow.Flags&ast.FlowFlagsNonNullish != 0 { - narrowedType = c.narrowTypeByTruthiness(f, c.narrowTypeByOptionality(f, nonEvolvingType, flow.Node, true /*assumePresent*/), flow.Node, false /*assumeTrue*/) + node := ast.SkipOuterExpressions(flow.Node, ast.OEKAll) + narrowedType = c.narrowTypeByTruthiness(f, c.narrowTypeByOptionality(f, nonEvolvingType, node, true /*assumePresent*/), node, false /*assumeTrue*/) } else { narrowedType = c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue) } diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt index 38523bc551b79..cc02a51cb1fab 100644 --- a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.errors.txt @@ -199,4 +199,16 @@ controlFlowNullishCoalescingFalsyLeft.ts(144,14): error TS2869: Right operand of (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; const one: 1 = b; } + + function f23(u: { tag: ""; empty: 1 } | { tag: "ok" }) { + if ((u.tag) ?? (false || true)) {} else { + u.empty; + } + } + + function f24(u: { tag: ""; empty: 1 } | { tag: "ok" }) { + if (u.tag! ?? (false || true)) {} else { + u.empty; + } + } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js index 55957eb18faf5..8cc1125b376d8 100644 --- a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.js @@ -161,6 +161,18 @@ function f22(a: "" | undefined) { (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; const one: 1 = b; } + +function f23(u: { tag: ""; empty: 1 } | { tag: "ok" }) { + if ((u.tag) ?? (false || true)) {} else { + u.empty; + } +} + +function f24(u: { tag: ""; empty: 1 } | { tag: "ok" }) { + if (u.tag! ?? (false || true)) {} else { + u.empty; + } +} //// [controlFlowNullishCoalescingFalsyLeft.js] @@ -293,3 +305,15 @@ function f22(a) { (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; const one = b; } +function f23(u) { + if ((u.tag) ?? (false || true)) { } + else { + u.empty; + } +} +function f24(u) { + if (u.tag ?? (false || true)) { } + else { + u.empty; + } +} diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols index b349da91fdf91..4ea501092d036 100644 --- a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.symbols @@ -361,3 +361,41 @@ function f22(a: "" | undefined) { >b : Symbol(b, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 156, 7)) } +function f23(u: { tag: ""; empty: 1 } | { tag: "ok" }) { +>f23 : Symbol(f23, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 159, 1)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 13)) +>tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 17)) +>empty : Symbol(empty, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 26)) +>tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 41)) + + if ((u.tag) ?? (false || true)) {} else { +>u.tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 17), Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 41)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 13)) +>tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 17), Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 41)) + + u.empty; +>u.empty : Symbol(empty, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 26)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 13)) +>empty : Symbol(empty, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 161, 26)) + } +} + +function f24(u: { tag: ""; empty: 1 } | { tag: "ok" }) { +>f24 : Symbol(f24, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 165, 1)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 13)) +>tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 17)) +>empty : Symbol(empty, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 26)) +>tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 41)) + + if (u.tag! ?? (false || true)) {} else { +>u.tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 17), Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 41)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 13)) +>tag : Symbol(tag, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 17), Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 41)) + + u.empty; +>u.empty : Symbol(empty, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 26)) +>u : Symbol(u, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 13)) +>empty : Symbol(empty, Decl(controlFlowNullishCoalescingFalsyLeft.ts, 167, 26)) + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types index d86120050142b..bd98907649be8 100644 --- a/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types +++ b/tsc/testdata/baselines/reference/compiler/controlFlowNullishCoalescingFalsyLeft.types @@ -504,3 +504,53 @@ function f22(a: "" | undefined) { >b : 1 } +function f23(u: { tag: ""; empty: 1 } | { tag: "ok" }) { +>f23 : (u: { tag: ""; empty: 1; } | { tag: "ok"; }) => void +>u : { tag: ""; empty: 1; } | { tag: "ok"; } +>tag : "" +>empty : 1 +>tag : "ok" + + if ((u.tag) ?? (false || true)) {} else { +>(u.tag) ?? (false || true) : "" | "ok" +>(u.tag) : "" | "ok" +>u.tag : "" | "ok" +>u : { tag: ""; empty: 1; } | { tag: "ok"; } +>tag : "" | "ok" +>(false || true) : true +>false || true : true +>false : false +>true : true + + u.empty; +>u.empty : 1 +>u : { tag: ""; empty: 1; } +>empty : 1 + } +} + +function f24(u: { tag: ""; empty: 1 } | { tag: "ok" }) { +>f24 : (u: { tag: ""; empty: 1; } | { tag: "ok"; }) => void +>u : { tag: ""; empty: 1; } | { tag: "ok"; } +>tag : "" +>empty : 1 +>tag : "ok" + + if (u.tag! ?? (false || true)) {} else { +>u.tag! ?? (false || true) : "" | "ok" +>u.tag! : "" | "ok" +>u.tag : "" | "ok" +>u : { tag: ""; empty: 1; } | { tag: "ok"; } +>tag : "" | "ok" +>(false || true) : true +>false || true : true +>false : false +>true : true + + u.empty; +>u.empty : 1 +>u : { tag: ""; empty: 1; } +>empty : 1 + } +} + diff --git a/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts b/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts index d0c7fe76675e1..00536f77ffa17 100644 --- a/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts +++ b/tsc/testdata/tests/cases/compiler/controlFlowNullishCoalescingFalsyLeft.ts @@ -162,3 +162,15 @@ function f22(a: "" | undefined) { (a ?? (b = 1, ""))?.charAt(b = 1) ?? false; const one: 1 = b; } + +function f23(u: { tag: ""; empty: 1 } | { tag: "ok" }) { + if ((u.tag) ?? (false || true)) {} else { + u.empty; + } +} + +function f24(u: { tag: ""; empty: 1 } | { tag: "ok" }) { + if (u.tag! ?? (false || true)) {} else { + u.empty; + } +}