Skip to content

String literal apparent types with specific member types #19846

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

Conversation

weswigham
Copy link
Member

As mentioned in #19837, calculating the apparent type of union members correctly exposed some undesired behavior with respect to the contextual type produced by the union of a string literal type and an array. The root cause is the index signature in the global string type; so another way to fix the issue is to give string literal type more specific apparent types than the global string type. In this PR, I create a cached apparent type (similar to what we do for intersections) for string literal types which has exact types for the 0 through N properties of the string, based on the text of the literal, and then a more specific index signature based on those types. This also has the nice sideffect of giving you stronger types for ("foo")[2] ("o") or keyof "someLiteral" (all the names on String plus "0" - N; up to the length of the literal.). Only doing this for the apparent type saves us from comparing these properties during relationships, while still giving us the benefits of stronger property types than the indexer on the original apparent type gives.

Fixes #19837

@weswigham
Copy link
Member Author

@mhegazy You'd probably want to take a look at this alongside my comment on the associated issue.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 12, 2018

Thanks for your contribution. This PR has not been updated in a while and cannot be automatically merged at the time being. For housekeeping purposes we are closing stale PRs. If you'd still like to continue working on this PR, please leave a message and one of the maintainers can reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fails to get contextual type for string literal in array
2 participants