Skip to content

Discriminate jsx contextual types same as object contextual types - #27408

Merged
Wesley Wigham (weswigham) merged 5 commits into
microsoft:masterfrom
weswigham:react-union-sfc-contextualtype-test
Oct 5, 2018
Merged

Wesley Wigham (weswigham) merged 5 commits into
microsoft:masterfrom
weswigham:react-union-sfc-contextualtype-test

Conversation

@weswigham

Copy link
Copy Markdown
Member

Fixes #26004

@RyanCavanaugh

Copy link
Copy Markdown
Member

Might fix #27211 as well?

Comment thread src/compiler/checker.ts Outdated
Comment thread src/compiler/checker.ts
}

/**
* Woah! Do you really want to use this function?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pretty sure it's spelled "Whoa" Daniel Rosenwasser (@DanielRosenwasser)

Comment thread src/compiler/checker.ts Outdated
return contextualType;
}
function discriminateContextualTypeByObjectMembers(node: ObjectLiteralExpression, contextualType: UnionType) {
// Keep the below up-to-date with the work done within `isRelatedTo` by `findMatchingDiscriminantType`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this applies to two functions now, enumerate the functions.

Actually, if you can extract the core of this algorithm and call it, that would be better. I'm not sure what you can do with that hilarious use of break propLoop in there. Maybe that could become a return?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this fix, but can you see whether removal of some of the duplication is possible? Otherwise we'll now have 3 copies of this one weird algorithm.

@sandersn

Copy link
Copy Markdown
Member

Looks like there is some assertion lint.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Couple of minor suggestions.

Comment thread src/compiler/checker.ts Outdated
}
}
if (possiblyDiscriminant) {
const result = getDiscriminationResultForProperty(contextualType, discriminatingType!, prop.symbol.escapedName, match, isTypeAssignableTo);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could get rid of the nullable assertion if you dropped possiblyDiscriminant and checked discriminatingType above. The no-initialiser case would need to change to discriminatingType = !!isDiscr... && trueType.

Not sure if it's worth the change. Probably?

Comment thread src/compiler/checker.ts Outdated
@weswigham

Copy link
Copy Markdown
Member Author

Nathan Shively-Sanders (@sandersn) I did better; you probably wanna look again (again), since AFAIK none of your last set of comments apply anymore.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This version is ok, although to be honest I liked the previous one a little better. This one makes me wonder if passing tuples of closures is a bit slow, whether we should measure it, etc.

@weswigham
Wesley Wigham (weswigham) merged commit 07dbd8b into microsoft:master Oct 5, 2018
@weswigham
Wesley Wigham (weswigham) deleted the react-union-sfc-contextualtype-test branch October 5, 2018 22:11
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants