Infer to partially homomorphic types (such as Pick<T, K>)#29787
Infer to partially homomorphic types (such as Pick<T, K>)#29787ahejlsberg merged 7 commits intomasterfrom
Conversation
src/compiler/checker.ts
Outdated
| // enables us to make meaningful inferences when the target is a Pick<T, K>). Otherwise, infer | ||
| // from a union of the property types in the source to the template type X. | ||
| const extendedConstraint = getConstraintOfType(constraintType); | ||
| if (extendedConstraint && extendedConstraint.flags & TypeFlags.Index) { |
There was a problem hiding this comment.
We should probably consider handling unions of indexes here.
There was a problem hiding this comment.
Specifically for along the lines of:
declare function f20<A, B, K extends keyof A | keyof B>(obj: Pick<A & B, K>): T;
which instantiable of the original f20 could easily produce.
There was a problem hiding this comment.
We don't do that for ordinary homomorphic types so I'm not sure why we'd do that here. Also, not clear how we would divide the inferences between A and B.
There was a problem hiding this comment.
We do do that for ordinary homomorphic mapped types. It's why constraintType is passed into inferFromMappedTypeConstraint.
There was a problem hiding this comment.
Like, in this inference function we break down keyof A | keyof B, and we break down (T extends keyof A) | (U extends keyof B), I don't see why we would not also break down T extends (keyof A | keyof B).
There was a problem hiding this comment.
- if (extendedConstraint && extendedConstraint.flags & TypeFlags.Index) {
- inferToHomomorphicMappedType(source, target, <IndexType>extendedConstraint);
- return true;
+ if (extendedConstraint) {
+ return inferFromMappedTypeConstraint(source, target, extendedConstraint);maybe? Probably shouldn't return, since making inferences directly to the constrained type parameter as well as the things referenced in its constraint should both be possible.
There was a problem hiding this comment.
I see what you're getting at. We really need to repeat the whole process for the constraint.
With this PR we improve type inference for partially homomorphic types such as
Pick<T, K>. Now, when inferring to a mapped type{ [P in K]: X }, whereKis a type parameterK extends keyof T, we make the same inferences as we would for a homomorphic mapped type{ [P in keyof T]: X }.Fixes #29765.