Distinguish between non-distributed and distributed type parameters - #64237
Distinguish between non-distributed and distributed type parameters#64237Anders Hejlsberg (ahejlsberg) wants to merge 12 commits into
Conversation
|
TypeScript Bot (@typescript-bot) perf test this faster |
|
Anders Hejlsberg (@ahejlsberg) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
TypeScript Bot (@typescript-bot) user test this |
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the user tests with tsc comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Something interesting changed - please have a look. Details
|
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the top 1000 repos with tsc comparing Something interesting changed - please have a look. Details
|
|
TypeScript Bot (@typescript-bot) test top1000 |
|
TypeScript Bot (@typescript-bot) user test this |
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the user tests with tsc comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
Co-authored-by: ahejlsberg <4226954+ahejlsberg@users.noreply.github.com>
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the top 1000 repos with tsc comparing Something interesting changed - please have a look. Details
|
|
Jeez - I think we need to close the coverage gap between RWC and our own test suite. Probably worth adding tests for things like the |
|
To be fair, homomorphic mapped types distribute too and they suffer from the same issue today. I played a little bit with closing that gap here: fix-63708...Andarist:mapped-type-distribution I'm not entirely sure if all the breaks in the above are correct though. With the version above this one errors when today it doesnt - but it does seem to me that a suspicious indexed access is allowed here today: declare function f<T, U extends T>(y: { [P in keyof T]: U[P] }): {
[P in keyof T]: U[P];
};
type AB = { a: 1 } | { b: 2 };
type A = { a: 1 };
const result = f<AB, A>({ b: 123 });
// ^? { a: 1 } | { b: unknown; } |
|
TypeScript Bot (@typescript-bot) user test this |
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the user tests with tsc comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the top 1000 repos with tsc comparing Something interesting changed - please have a look. Details
|
|
User tests are clean and top 1000 has three failing tests, two of which are correct new errors. That leaves the new errors in |
|
Mateusz Burzyński (@Andarist) was chatting with Anders Hejlsberg (@ahejlsberg) about the implementation here, and one of the points that came up was the fact that |
|
Daniel Rosenwasser (@DanielRosenwasser) I was skeptical at first of expanding the scope of And isn't this just a new kind of wrapper that has to be dereferenced? The problem at hand is specifically the reverse mapped type inference where the inference target is I experimented with the dokploy regression and at least some of that can be narrowed down to exactly that problem :p Just this time it's about Of course, maybe there is a counterargument to all of that and a different solution to account for the reverse mapped types but that one seemed right to me at the time of preparing that experiment |
|
Mateusz Burzyński (@Andarist) I went through and examined all the places we use |
|
Mateusz Burzyński (@Andarist) Borrowed your regression test, thanks for researching. |
|
TypeScript Bot (@typescript-bot) user test this |
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the user tests with tsc comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
|
Tests are clean. Two top 1000 projects have new errors, but those errors are expected. |
| if t.flags&TypeFlagsTypeParameter != 0 && !t.AsTypeParameter().isDistributed { | ||
| for n := node.Parent; n != nil && !ast.IsStatement(n); n = n.Parent { | ||
| if ast.IsConditionalTypeNode(n) { | ||
| if checkTypeNode := n.AsConditionalTypeNode().CheckType; isSimpleIdentifierTypeReference(checkTypeNode) && c.getSymbolFromTypeReference(checkTypeNode) == t.symbol { |
There was a problem hiding this comment.
Why not c.getTypeFromTypeNode(n).AsConditionalType().root.isDistributive, so the distribution-choice logic stays centralized?
With this PR we distinguish between non-distributed and distributed type parameters in conditional types. Previously we didn't which could lead to constraint violations. In the example
we previously didn't report a constraint violation on
Show<A, B>. We now report a new errorA type parameter is considered to be distributed when it is referenced within a conditional type that distributes that type parameter. Specfically, in a conditional type
T extends XXX ? AAA : BBB,Tis considered distributed within the typesXXX,AAA, andBBB.The distributed form of a type parameter is a subtype of the non-distributed form of that type parameter, and behaves accordingly. In the
Issue<A, B extends A>type above,Bis known to extend the non-distributedA, but that doesn't imply it extends the distributedA, which is a subtype of the non-distributedA. For example, the typeIssue<0 | 1, 0>creates a union ofShow<0, 0>andShow<1, 0>, the second of which violates the constraints declared inShow<T, U extends T>.The language service now shows
(distributed)when hovering over distributed type parameters:This makes it easier to recognize when a type parameter is distributed.
Fixes #63708.