-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type narrowing does not work when accessing a Partial<Record> with a Union type as the key #54003
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
Comments
There's no narrowing going on in the bottom example. Duplicate #48227, etc |
Ah I see. I forgot about the @RyanCavanaugh I tried looking through the FAQ and it sounds like this has come up a lot. Did I miss a question that covers this? Is it valid to consider this a bug? |
This should be in the FAQ. I would say this is really more of a feature request, since to do it soundly requires tracking information that is currently not considered. Conservative implementations of this have also incurred too-large performance hits, so it's a trade-off. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
2 similar comments
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
type narrowing, union, if, undefined, partial
🕗 Version & Regression Information
I saw this initially with 5.0.4 I used the playground below to try with other versions as old as 3.3
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
When checking if a key exists on a
Partial<Record>
with a key value type set to a union of string literals, the type of the value is not narrowed down in the same way as it is for aRecord
with astring
key in a similar situation.🙂 Expected behavior
When checking if a key exists on a
Partial<Record>
with a key value type set to a union of string literals, the type of the value is narrowed to exclude undefinedThe text was updated successfully, but these errors were encountered: