From 1d6f016139acf694870c7fdf252c1762de790de6 Mon Sep 17 00:00:00 2001 From: cplieger <917744+cplieger@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:07:21 +0100 Subject: [PATCH 1/3] fix: add a false path for a falsy, non-nullish left operand of ?? --- tsc/internal/binder/binder.go | 21 ++++++++++ tsc/internal/checker/flow.go | 3 ++ ...hCoalescingFalsyLeftOperandFlow.errors.txt | 39 +++++++++++++++++++ .../nullishCoalescingFalsyLeftOperandFlow.ts | 14 +++++++ 4 files changed, 77 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt create mode 100644 tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts diff --git a/tsc/internal/binder/binder.go b/tsc/internal/binder/binder.go index e1058bc33b043..0dda5ebc8847d 100644 --- a/tsc/internal/binder/binder.go +++ b/tsc/internal/binder/binder.go @@ -10,6 +10,7 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/core" "github.com/microsoft/TypeScript/tsc/internal/debug" "github.com/microsoft/TypeScript/tsc/internal/diagnostics" + "github.com/microsoft/TypeScript/tsc/internal/jsnum" "github.com/microsoft/TypeScript/tsc/internal/scanner" ) @@ -2304,6 +2305,9 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL } else { b.bindCondition(expr.Left, trueTarget, preRightLabel) } + if (expr.OperatorToken.Kind == ast.KindQuestionQuestionToken || expr.OperatorToken.Kind == ast.KindQuestionQuestionEqualsToken) && canBeFalsyNonNullish(expr.Left) && !ast.IsLogicalExpression(ast.WalkUpParenthesizedExpressions(node.Parent)) { + b.addAntecedent(falseTarget, b.createFlowCondition(ast.FlowFlagsCondition, b.currentFlow, expr.Left)) + } b.currentFlow = b.finishFlowLabel(preRightLabel) b.bind(expr.OperatorToken) if ast.IsLogicalOrCoalescingAssignmentOperator(expr.OperatorToken.Kind) { @@ -2316,6 +2320,23 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL } } +func canBeFalsyNonNullish(node *ast.Node) bool { + node = ast.SkipOuterExpressions(node, ast.OEKAll) + switch node.Kind { + case ast.KindFalseKeyword, ast.KindThisKeyword, ast.KindPropertyAccessExpression, ast.KindElementAccessExpression, ast.KindTaggedTemplateExpression, ast.KindAwaitExpression, ast.KindYieldExpression: + return true + case ast.KindCallExpression: + return !ast.IsImportCall(node) + case ast.KindIdentifier: + return node.Text() != "undefined" + case ast.KindNumericLiteral, ast.KindBigIntLiteral: + return jsnum.ParsePseudoBigInt(node.Text()) == "0" + case ast.KindStringLiteral, ast.KindNoSubstitutionTemplateLiteral: + return node.Text() == "" + } + return false +} + 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..1d2cee0502604 100644 --- a/tsc/internal/checker/flow.go +++ b/tsc/internal/checker/flow.go @@ -366,6 +366,9 @@ func (c *Checker) getTypeAtFlowCondition(f *FlowState, flow *ast.FlowNode) FlowT assumeTrue := flow.Flags&ast.FlowFlagsTrueCondition != 0 nonEvolvingType := c.finalizeEvolvingArrayType(flowType.t) narrowedType := c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue) + if flow.Flags&ast.FlowFlagsCondition == ast.FlowFlagsCondition { + narrowedType = c.narrowTypeByTruthiness(f, narrowedType, flow.Node, false) + } if narrowedType == nonEvolvingType { return flowType } diff --git a/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt new file mode 100644 index 0000000000000..06bacc62beefa --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt @@ -0,0 +1,39 @@ +nullishCoalescingFalsyLeftOperandFlow.ts(4,24): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +nullishCoalescingFalsyLeftOperandFlow.ts(4,50): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(5,24): error TS2871: This expression is always nullish. +nullishCoalescingFalsyLeftOperandFlow.ts(5,50): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(6,25): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. +nullishCoalescingFalsyLeftOperandFlow.ts(6,59): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(7,60): error TS7027: Unreachable code detected. +nullishCoalescingFalsyLeftOperandFlow.ts(8,112): error TS2322: Type '"" | undefined' is not assignable to type 'undefined'. + Type '""' is not assignable to type 'undefined'. + + +==== nullishCoalescingFalsyLeftOperandFlow.ts (8 errors) ==== + declare function c(x?: undefined): boolean; + + function f1() { while (c() ?? (c() || true)) {} c(); } + function f2() { while (true ?? (c() || true)) {} c(); } + ~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + ~~~~ +!!! error TS7027: Unreachable code detected. + function f3() { while (null ?? (c() || true)) {} c(); } + ~~~~ +!!! error TS2871: This expression is always nullish. + ~~~~ +!!! error TS7027: Unreachable code detected. + function f4() { while ((c() ? 1 : 2) ?? (c() || true)) {} c(); } + ~~~~~~~~~~~ +!!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. + ~~~~ +!!! error TS7027: Unreachable code detected. + function f5() { while ((c() || null)! ?? (c() || true)) {} c(); } + ~~~~ +!!! error TS7027: Unreachable code detected. + function f6(a: "" | object | undefined, b: boolean) { if (a ?? b) {} else { const s: "" | undefined = a; const u: undefined = a; } ((a ?? b) || b) ?? c(a); } + ~ +!!! error TS2322: Type '"" | undefined' is not assignable to type 'undefined'. +!!! error TS2322: Type '""' is not assignable to type 'undefined'. + function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } + \ No newline at end of file diff --git a/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts new file mode 100644 index 0000000000000..3046755148e5f --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts @@ -0,0 +1,14 @@ +// @strict: true +// @allowUnreachableCode: false +// @noEmit: true +// @noTypesAndSymbols: true + +declare function c(x?: undefined): boolean; + +function f1() { while (c() ?? (c() || true)) {} c(); } +function f2() { while (true ?? (c() || true)) {} c(); } +function f3() { while (null ?? (c() || true)) {} c(); } +function f4() { while ((c() ? 1 : 2) ?? (c() || true)) {} c(); } +function f5() { while ((c() || null)! ?? (c() || true)) {} c(); } +function f6(a: "" | object | undefined, b: boolean) { if (a ?? b) {} else { const s: "" | undefined = a; const u: undefined = a; } ((a ?? b) || b) ?? c(a); } +function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } From 6fda82e39381d47c77172a67405160ed35d31f86 Mon Sep 17 00:00:00 2001 From: cplieger <917744+cplieger@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:09:55 +0100 Subject: [PATCH 2/3] fix: keep the rest of an optional chain on a falsy ?? base --- tsc/internal/binder/binder.go | 3 ++- .../compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt | 2 ++ .../cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tsc/internal/binder/binder.go b/tsc/internal/binder/binder.go index 0dda5ebc8847d..1b6c633fecc3c 100644 --- a/tsc/internal/binder/binder.go +++ b/tsc/internal/binder/binder.go @@ -2305,7 +2305,8 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL } else { b.bindCondition(expr.Left, trueTarget, preRightLabel) } - if (expr.OperatorToken.Kind == ast.KindQuestionQuestionToken || expr.OperatorToken.Kind == ast.KindQuestionQuestionEqualsToken) && canBeFalsyNonNullish(expr.Left) && !ast.IsLogicalExpression(ast.WalkUpParenthesizedExpressions(node.Parent)) { + parent := ast.WalkUpParenthesizedExpressions(node.Parent) + if (expr.OperatorToken.Kind == ast.KindQuestionQuestionToken || expr.OperatorToken.Kind == ast.KindQuestionQuestionEqualsToken) && canBeFalsyNonNullish(expr.Left) && !ast.IsLogicalExpression(parent) && !(ast.IsOptionalChain(parent) && ast.SkipParentheses(parent.Expression()) == node) { b.addAntecedent(falseTarget, b.createFlowCondition(ast.FlowFlagsCondition, b.currentFlow, expr.Left)) } b.currentFlow = b.finishFlowLabel(preRightLabel) diff --git a/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt index 06bacc62beefa..67f1316dc2209 100644 --- a/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt @@ -36,4 +36,6 @@ nullishCoalescingFalsyLeftOperandFlow.ts(8,112): error TS2322: Type '"" | undefi !!! error TS2322: Type '"" | undefined' is not assignable to type 'undefined'. !!! error TS2322: Type '""' is not assignable to type 'undefined'. function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } + function f8(a: "" | undefined) { let b: 0 | 1 = 0; if ((a ?? (b = 1, ""))?.[b = 1]) {} else { const one: 1 = b; } } + function f9(a: ((x: 1) => boolean) | undefined, d: (x: 1) => boolean) { let b: 0 | 1 = 0; if ((a ?? (b = 1, d))?.(b = 1)) {} else { const one: 1 = b; } } \ No newline at end of file diff --git a/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts index 3046755148e5f..f34dc176dd565 100644 --- a/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts +++ b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts @@ -12,3 +12,5 @@ function f4() { while ((c() ? 1 : 2) ?? (c() || true)) {} c(); } function f5() { while ((c() || null)! ?? (c() || true)) {} c(); } function f6(a: "" | object | undefined, b: boolean) { if (a ?? b) {} else { const s: "" | undefined = a; const u: undefined = a; } ((a ?? b) || b) ?? c(a); } function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } +function f8(a: "" | undefined) { let b: 0 | 1 = 0; if ((a ?? (b = 1, ""))?.[b = 1]) {} else { const one: 1 = b; } } +function f9(a: ((x: 1) => boolean) | undefined, d: (x: 1) => boolean) { let b: 0 | 1 = 0; if ((a ?? (b = 1, d))?.(b = 1)) {} else { const one: 1 = b; } } From f73e756b3d9b1273b83a2cad807ec847d00dcbdb Mon Sep 17 00:00:00 2001 From: cplieger <917744+cplieger@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:43:26 +0100 Subject: [PATCH 3/3] fix: narrow the left operand of ?? through wrappers the binder skips --- tsc/internal/checker/flow.go | 3 ++- .../compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt | 2 ++ .../cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tsc/internal/checker/flow.go b/tsc/internal/checker/flow.go index 1d2cee0502604..8ad08621eebaa 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) narrowedType := c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue) if flow.Flags&ast.FlowFlagsCondition == ast.FlowFlagsCondition { - narrowedType = c.narrowTypeByTruthiness(f, narrowedType, flow.Node, false) + node := ast.SkipOuterExpressions(flow.Node, ast.OEKAll) + narrowedType = c.narrowTypeByTruthiness(f, c.narrowTypeByOptionality(f, nonEvolvingType, node, true), node, false) } if narrowedType == nonEvolvingType { return flowType diff --git a/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt index 67f1316dc2209..a83876cc1b961 100644 --- a/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/nullishCoalescingFalsyLeftOperandFlow.errors.txt @@ -38,4 +38,6 @@ nullishCoalescingFalsyLeftOperandFlow.ts(8,112): error TS2322: Type '"" | undefi function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } function f8(a: "" | undefined) { let b: 0 | 1 = 0; if ((a ?? (b = 1, ""))?.[b = 1]) {} else { const one: 1 = b; } } function f9(a: ((x: 1) => boolean) | undefined, d: (x: 1) => boolean) { let b: 0 | 1 = 0; if ((a ?? (b = 1, d))?.(b = 1)) {} else { const one: 1 = b; } } + function f10(u: { tag: ""; empty: 1 } | { tag: "ok" }) { if ((u.tag) ?? (false || true)) {} else { u.empty; } } + function f11(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/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts index f34dc176dd565..c23d32b978c15 100644 --- a/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts +++ b/tsc/testdata/tests/cases/compiler/nullishCoalescingFalsyLeftOperandFlow.ts @@ -14,3 +14,5 @@ function f6(a: "" | object | undefined, b: boolean) { if (a ?? b) {} else { cons function f7(a: "" | boolean | undefined, b: boolean) { if (a ??= b) {} else { a === ""; } } function f8(a: "" | undefined) { let b: 0 | 1 = 0; if ((a ?? (b = 1, ""))?.[b = 1]) {} else { const one: 1 = b; } } function f9(a: ((x: 1) => boolean) | undefined, d: (x: 1) => boolean) { let b: 0 | 1 = 0; if ((a ?? (b = 1, d))?.(b = 1)) {} else { const one: 1 = b; } } +function f10(u: { tag: ""; empty: 1 } | { tag: "ok" }) { if ((u.tag) ?? (false || true)) {} else { u.empty; } } +function f11(u: { tag: ""; empty: 1 } | { tag: "ok" }) { if (u.tag! ?? (false || true)) {} else { u.empty; } }