Skip to content

Apply isLiteralContextualType recursively for unions and intersections. #19684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

kpdonn
Copy link
Contributor

@kpdonn kpdonn commented Nov 2, 2017

Fixes #19632. The issue was isLiteralContextualType was only checking
for type parameters that extended a literal base type at the top level.
It was already recursively checking the constituents of unions and
intersections but it did so using maybeTypeOfKind which only compared
type flags matching literal or index.

Also adds a test case that failed before this change and succeeds now.

I realize this wasn't an issue marked as "help wanted" so I understand the PR might not be accepted. I was just curious why the issue I was having was happening and figured it'd be a good way to learn more about how Typescript worked internally, and once I figured out why it was happening it looked easy to fix so I figured I'd try.

Fixes microsoft#19632. The issue was isLiteralContextualType was only checking
for type parameters that extended a literal base type at the top level.
It was already recursively checking the constituents of unions and
intersections but it did so using maybeTypeOfKind which only compared
type flags matching literal or index.

Also adds a test case that failed before this change and succeeds now.
@msftclas
Copy link

msftclas commented Nov 2, 2017

CLA assistant check
All CLA requirements met.

@@ -18458,7 +18458,17 @@ namespace ts {
}
contextualType = constraint;
}
return maybeTypeOfKind(contextualType, (TypeFlags.Literal | TypeFlags.Index));
Copy link
Contributor

Choose a reason for hiding this comment

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

return forEachType(contextualType, t => t.flags & (TypeFlags.Literal | TypeFlags.Index));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mhegazy That wouldn't accomplish the same thing as my change. maybeTypeOfKind actually already did something very similar to what you propose, except that it looked for UnionOrIntersectionType for the recursion while forEachType only looks for UnionType.

The problem is the behavior from #19632 was caused by not extracting the any generic constraint types during the recursion into the unions and intersections.

I did go ahead and refactor the code I wrote to use the core forEach function, and also added a few more test cases.

The test cases cover more scenarios that failed before this change and
work now, plus added a couple of tests where the code is still expected
to have compile errors because the generics do not extend string or
number just to verify that my change did not affect that behavior.
@kpdonn
Copy link
Contributor Author

kpdonn commented Mar 1, 2018

Closing because the problem this was addressing was fixed by something else at some point.

@kpdonn kpdonn closed this Mar 1, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
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.

String literal generic not always inferred when part of union in nested object
3 participants