Skip to content

Type narrowing fails for property access when using comma operator #41625

@dummdidumm

Description

@dummdidumm

TypeScript Version: 4.2.0-dev.20201121
Search Terms: comma operator flow narrow type function property access

I encountered a case where type narrowing fails when using the comma operator and accessing a property of the last operator in the chain afterwards. It does narrow the type correctly when using the value directly, but when using that value with a comma operator it does not.

Code

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

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

if (typeof (otherValue(), value).inner === 'number') {
    const a = value.inner; // number
    const b = (otherValue(), value).inner; // string | number , but should be number
}

if (isNumber((otherValue(), value).inner)) {
    const a = value.inner; // number
    const b = (otherValue(), value).inner; // string | number , but should be number
}

Expected behavior:
I would expect the second case (variable b) 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?ts=4.2.0-dev.20201121#code/MYewdgzgLgBCUAsCmAnAagQwDYFckwF4YAKASkID4YoU8BuAWAChRJYA3bPGALhgG8AlmDCo+YHAFsARqhgAfGNBTCA5gF9CMCViwwMEfWACejJswBmOMMCiDwMQRAByU2SmIhpAKz4YTpHxe3o6GEjJy-MwwMTAoSFA4KGDUtEh0MAD0mTCSCQggACaOkgAOWEh5YFAYdg6CKPEVnNXM6szMghYkUMalSCDdnoiomLhIZAA0MJzjpAB0wqIohAREAOTh7uvkUUyxMKzQ+lqzeIsiqBnZ2m6o0bFHsNJaw8joXBOk02dIC0tXLI5ZRqBS3CIrabSHCwCAFHBYYqycHuNodJhdEhOVwQ4hvUafKYzT7-S4oUi7B4xJ4nIi-C7La45Lb3faPcDHF5EfEfcZE36kxlApQ0UGKFmQmDQ2HwxFS-AStFMIA

Related Issues:
#41183

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions