Skip to content

Type narrowing fails when using comma operator for function parameter #41183

@dummdidumm

Description

@dummdidumm

TypeScript Version: 4.0.2
Search Terms: comma operator flow narrow type function

I encountered a case where type narrowing fails when using the comma operator and passing the result to a function which does type narrowing.

Code

const otherValue = () => true;
const value : number | string = null as any;

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

// Good case - works
if (typeof (otherValue(), value) === 'number') {
    const b = value; // number
}

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

Expected behavior:
I would expect the second case to also narrow down the type to number

Actual behavior:
Type is string | number, it was not narrowed.

Playground Link:
https://www.typescriptlang.org/play?#code/MYewdgzgLgBCUAsCmAnAagQwDYFckwF4YAKASkID4YoU8BuAKFElgDds8YAuGMHAWwBGqGAB8Y0FAEswAc0K8cWLDAwRVYAJ6MGAMxxhgUKeBhSIAOQHCUxEIIBWPDFtI97Ds+r5CRAbwYYIJgUJCgcFDBqWiQ6GAB6eJh+MIQQABMzfgAHLCQUsCgMY1MpFFC89kKGAF8GBildEihNbKQQJrtEVExcJDIAGhh2PvICcZgAch8bSfIA4JhmaBhBBRH6BKSZ1Fr6xpJzK19bLuR0Dn7SIY2kUnuYBeDl2DWiW7jEiRoZeXEdlC1IA

Related Issues:
Sounds a little related: #35484

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Control FlowThe issue relates to control flow analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions