Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ namespace ts {
function isNarrowableReference(expr: Expression): boolean {
return expr.kind === SyntaxKind.Identifier || expr.kind === SyntaxKind.PrivateIdentifier || expr.kind === SyntaxKind.ThisKeyword || expr.kind === SyntaxKind.SuperKeyword ||
(isPropertyAccessExpression(expr) || isNonNullExpression(expr) || isParenthesizedExpression(expr)) && isNarrowableReference(expr.expression) ||
isBinaryExpression(expr) && expr.operatorToken.kind === SyntaxKind.CommaToken && isNarrowableReference(expr.right) ||
isElementAccessExpression(expr) && isStringOrNumericLiteralLike(expr.argumentExpression) && isNarrowableReference(expr.expression) ||
isAssignmentExpression(expr) && isNarrowableReference(expr.left);
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20720,7 +20720,8 @@ namespace ts {
case SyntaxKind.NonNullExpression:
return isMatchingReference(source, (target as NonNullExpression | ParenthesizedExpression).expression);
case SyntaxKind.BinaryExpression:
return isAssignmentExpression(target) && isMatchingReference(source, target.left);
return (isAssignmentExpression(target) && isMatchingReference(source, target.left)) ||
(isBinaryExpression(target) && target.operatorToken.kind === SyntaxKind.CommaToken && isMatchingReference(source, target.right));
}
switch (source.kind) {
case SyntaxKind.Identifier:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [controlFlowCommaExpressionFunctionCall.ts]
const otherValue = () => true;
const value : number | string = null as any;

function isNumber(obj: any): obj is number {
return true; // method implementation irrelevant
}

// Bad case - fails
if (isNumber((otherValue(), value))) {
const b = value; // string | number , but should be number
}

//// [controlFlowCommaExpressionFunctionCall.js]
var otherValue = function () { return true; };
var value = null;
function isNumber(obj) {
return true; // method implementation irrelevant
}
// Bad case - fails
if (isNumber((otherValue(), value))) {
var b = value; // string | number , but should be number
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/controlFlowCommaExpressionFunctionCall.ts ===
const otherValue = () => true;
>otherValue : Symbol(otherValue, Decl(controlFlowCommaExpressionFunctionCall.ts, 0, 5))

const value : number | string = null as any;
>value : Symbol(value, Decl(controlFlowCommaExpressionFunctionCall.ts, 1, 5))

function isNumber(obj: any): obj is number {
>isNumber : Symbol(isNumber, Decl(controlFlowCommaExpressionFunctionCall.ts, 1, 44))
>obj : Symbol(obj, Decl(controlFlowCommaExpressionFunctionCall.ts, 3, 18))
>obj : Symbol(obj, Decl(controlFlowCommaExpressionFunctionCall.ts, 3, 18))

return true; // method implementation irrelevant
}

// Bad case - fails
if (isNumber((otherValue(), value))) {
>isNumber : Symbol(isNumber, Decl(controlFlowCommaExpressionFunctionCall.ts, 1, 44))
>otherValue : Symbol(otherValue, Decl(controlFlowCommaExpressionFunctionCall.ts, 0, 5))
>value : Symbol(value, Decl(controlFlowCommaExpressionFunctionCall.ts, 1, 5))

const b = value; // string | number , but should be number
>b : Symbol(b, Decl(controlFlowCommaExpressionFunctionCall.ts, 9, 9))
>value : Symbol(value, Decl(controlFlowCommaExpressionFunctionCall.ts, 1, 5))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/controlFlowCommaExpressionFunctionCall.ts ===
const otherValue = () => true;
>otherValue : () => boolean
>() => true : () => boolean
>true : true

const value : number | string = null as any;
>value : string | number
>null as any : any
>null : null

function isNumber(obj: any): obj is number {
>isNumber : (obj: any) => obj is number
>obj : any

return true; // method implementation irrelevant
>true : true
}

// Bad case - fails
if (isNumber((otherValue(), value))) {
>isNumber((otherValue(), value)) : boolean
>isNumber : (obj: any) => obj is number
>(otherValue(), value) : string | number
>otherValue(), value : string | number
>otherValue() : boolean
>otherValue : () => boolean
>value : string | number

const b = value; // string | number , but should be number
>b : number
>value : number
}
11 changes: 11 additions & 0 deletions tests/cases/compiler/controlFlowCommaExpressionFunctionCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const otherValue = () => true;
const value : number | string = null as any;

function isNumber(obj: any): obj is number {
return true; // method implementation irrelevant
}

// Bad case - fails
if (isNumber((otherValue(), value))) {
const b = value; // string | number , but should be number
}