-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Simplify object types when distributing index over them #60021
base: main
Are you sure you want to change the base?
Simplify object types when distributing index over them #60021
Conversation
@@ -18997,7 +18997,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||
// (T | U)[K] -> T[K] & U[K] (writing) | |||
// (T & U)[K] -> T[K] & U[K] | |||
if (objectType.flags & TypeFlags.Union || objectType.flags & TypeFlags.Intersection && !shouldDeferIndexType(objectType)) { | |||
const types = map((objectType as UnionOrIntersectionType).types, t => getSimplifiedType(getIndexedAccessType(t, indexType), writing)); | |||
const types = map((objectType as UnionOrIntersectionType).types, t => getSimplifiedType(getIndexedAccessType(getSimplifiedType(t, writing), indexType), writing)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's important to simplify the t
(a constituent of the objectType
) here "eagerly". At this stage getIndexedAccessType
has a chance to return unknownType
(thus effectively eliminating this constituent from the overall simplified indexed access type on an intersection, since T & unknown
is T
).
Without it, the compiler comes back to getSimplifiedIndexedAccessType
with the non-simplified indexed access. It simplifies the object type there, but it also decides there is nothing to simplify further so it returns the original non-simplified type
. That leads to a relationship error.
@typescript-bot test it |
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
@jakebailey Here are the results of running the user tests with tsc comparing Everything looks good! |
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
fixes #59946