Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tsc/internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -2304,6 +2305,10 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL
} else {
b.bindCondition(expr.Left, trueTarget, preRightLabel)
}
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)
b.bind(expr.OperatorToken)
if ast.IsLogicalOrCoalescingAssignmentOperator(expr.OperatorToken.Kind) {
Expand All @@ -2316,6 +2321,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)
Expand Down
4 changes: 4 additions & 0 deletions tsc/internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ 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 {
node := ast.SkipOuterExpressions(flow.Node, ast.OEKAll)
narrowedType = c.narrowTypeByTruthiness(f, c.narrowTypeByOptionality(f, nonEvolvingType, node, true), node, false)
}
if narrowedType == nonEvolvingType {
return flowType
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 === ""; } }
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; } }

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @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 === ""; } }
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; } }