Skip to content

Conversation

@Andarist
Copy link
Contributor

fixes #49643

It's a regression from: 78ef3e7

Previously the first part of the condition was returning true in the isTypeRelatedTo here (called from narrowTypeByDiscriminant by isTypeComparableTo):
https://github.dev/microsoft/TypeScript/blob/8636adbbb9ab47bb377bd89a3390a13b66df34d2/src/compiler/checker.ts#L18248-L18250

It was passing because unions were not ignored by this condition here:
78ef3e7#diff-d9ab6589e714c71e657f601cf30ff51dfc607fc98419bf72e04f6b0fa92cc4b8L17910

Since the mentioned commit explicitly started to ignore unions there and the comment was put in place about it the code followed along through this call:
https://github.dev/microsoft/TypeScript/blob/8636adbbb9ab47bb377bd89a3390a13b66df34d2/src/compiler/checker.ts#L18263-L18265
and a union member has started to be tested through the condition I've adjusted now. The fix was needed here because this condition was not checking the "reversed" relation (isSimpleTypeRelatedTo(originalTarget, originalSource, relation)) in the case of the comparableRelation.

cc @nicolo-ribaudo

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jun 23, 2022
// skip all the checks we don't need and just return `isSimpleTypeRelatedTo` result
if (originalSource.flags & TypeFlags.Object && originalTarget.flags & TypeFlags.Primitive) {
if (isSimpleTypeRelatedTo(originalSource, originalTarget, relation, reportErrors ? reportError : undefined)) {
if (relation === comparableRelation && !(originalTarget.flags & TypeFlags.Never) && isSimpleTypeRelatedTo(originalTarget, originalSource, relation) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Andarist Andarist force-pushed the fix/distriminating-union-with-union-prop-against-undefined branch from c01c5bc to 1727ccc Compare July 5, 2022 08:37
@weswigham weswigham merged commit a329210 into microsoft:main Nov 7, 2022
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Ineffective union discrimination based on object property

3 participants