-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve contextual typing of ending tuple elements #53036
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at b2f0543. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at b2f0543. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at b2f0543. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at b2f0543. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at b2f0543. You can monitor the build here. |
@ahejlsberg Here are the results of running the user test suite comparing Everything looks good! |
Heya @ahejlsberg, I've run the RWC suite on this PR - assuming you're on the TS core team, you can view the resulting diff here. |
@ahejlsberg Here are the results of running the top-repos suite comparing Everything looks good! |
@typescript-bot perf test faster |
Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at b2f0543. You can monitor the build here. Update: The results are in! |
@ahejlsberg Here they are:Comparison Report - main..53036
System
Hosts
Scenarios
Developer Information: |
src/compiler/checker.ts
Outdated
} | ||
// If element index is known and a contextual property with that name exists, return it. Otherwise return the | ||
// iterated or element type of the contextual type. | ||
return (!firstSpreadIndex || index < firstSpreadIndex) && getTypeOfPropertyOfContextualType(type, "" + index as __String) || |
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.
Maybe it should be treated as a separate issue... but I feel that perhaps this feature here might directly complicate things for that issue so it's at least worth mentioning now.
Take a look at this TS playground. The problem there is that getTypeOfPropertyOfContextualType
gets the getElementTypeOfSliceOfTupleType
, so we end up with a union of all element types instead of types of individual tuple members.
I prepared a quick fix for this issue in this PR: #53042 but it doesn't handle the case that is being fixed here (ending tuple elements).
The case outlined there is currently a little bit silly (T[K & keyof T]
within [K in keyof T2]: { ... }
) but I partially encountered this need when working on inference for intersected mapped types (PR here). This case would be better if rewritten like this:
diff --git a/tests/cases/compiler/contextuallyTypedElementsOfGenericZippingTuples.ts b/tests/cases/compiler/contextuallyTypedElementsOfGenericZippingTuples.ts
index a011321a26..c27c91930f 100644
--- a/tests/cases/compiler/contextuallyTypedElementsOfGenericZippingTuples.ts
+++ b/tests/cases/compiler/contextuallyTypedElementsOfGenericZippingTuples.ts
@@ -11,8 +11,8 @@ declare function test<T extends unknown[], T2 extends unknown[]>(
],
b: [
...{
- [K in keyof T2]: {
- consume: (arg: T[K & keyof T]) => T2[K];
+ [K in keyof T2 as K & keyof T]: {
+ consume: (arg: T[K]) => T2[K];
};
}
]
There are 2 problems with this code today though:
- such a reverse mapped type isn't inferred at all (a PR fixing this problem can be found here)
- this isn't recognized as a tuple type so TS reports
A rest element type must be an array type.ts(2574)
. That probably could be improved as well.
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.
@ahejlsberg Any concerns given the above?
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.
No, that's a separate issue.
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.
Yes, I just meant specifically the "this feature here might directly complicate things for that issue so it's at least worth mentioning now" bit.
# Conflicts: # src/compiler/checker.ts
# Conflicts: # src/compiler/checker.ts
This PR improves the precision of contextual types originating in ending elements of contextual tuple types. For example:
Previously, the arrow function parameters would just be given an implicit
any
type.Fixes #43122.
Fixes #52846.