-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve apparent type of mapped types with mapped type modifiers #58091
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
const target = (type.target ?? type) as MappedType; | ||
const typeVariable = getHomomorphicTypeVariable(target); | ||
if (typeVariable && !target.declaration.nameType) { | ||
const constraint = getConstraintTypeFromMappedType(type); | ||
if (constraint.flags & TypeFlags.Index) { | ||
const baseConstraint = getBaseConstraintOfType((constraint as IndexType).type); | ||
if (baseConstraint && everyType(baseConstraint, t => isArrayOrTupleType(t) || isArrayOrTupleOrIntersection(t))) { | ||
return instantiateType(target, prependTypeMapping(typeVariable, baseConstraint, type.mapper)); |
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.
The type.mapper
here looks something like:
m1: T_id -> Box<string>[]
m2: m1: K -> K
m2: m1: T_id -> UnboxArray<T_outer>
m2: T_id -> UnboxArray<T_outer>
Since typeVariable
is T_id
TS ended up "skipping" over the inner mapping of UnboxArray
. The constraint
(of the type
) is keyof T_outer
with a baseConstraint
of Box<string>[]
.
In a sense, I'd like to map T_outer
to baseConstraint
(Box<string>[]
), process that through type.mapper
and then map T_id
to that result (or smth along those lines). I couldn't find a clear way to do that given that we need to account for non-homomorphic instantiations of homomorphic mapped types here. Using the modifiers type does seem to do the trick but I'm not overly confident in this solution and I won't mind being proven that this is wrong 😉
@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 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 comparing Everything looks good! |
Damn, I was close! :P It was worth a shot :) |
fixes #58060