-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unlisted properties type narrowing seems to be working only for actual property access but the type is still incorrect on the object itself #51739
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
It's a more general problem - not directly related to the new narrowing capabilities of TypeScript only narrows down property types but this doesn't affect their parent object types at all. Notice how in this playground the type of |
I see, thanks for details! |
Bug Report
With the release of TS 4.9 type narrowing was improved and we can use it to check for presence of some certain keys and their types in unknown objects, which works fine if we were to access the property like
foo.bar
and it would be, for example, astring
. However, the type is incorrect for the object itself even after checking for the property's type and would result inobject & Record<"bar", unknown>
.🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Object doesn't infer types after unlisted property type checking, i.e.
object & Record<"bar", unknown>
🙂 Expected behavior
Object should know the type of a key after unlisted property type check, i.e.
object & Record<"bar", string>
The text was updated successfully, but these errors were encountered: