Merged
Conversation
1. `T[K]` now correctly produces `number` when `K extends string, T extends Record<K, number>`. 2. `T[K]` no longer allows any type to be assigned to it when `T extends object, K extends keyof T`. Previously both of these cases failed in getConstraintOfIndexedAccessType because both bases followed `K`'s base constraint to `string` and then incorrectly produced `any` for types (like `object`) with no string index signature. In (1), this produced an error in checkBinaryLikeExpression`. In (2), this failed to produce an error in `checkTypeRelatedTo`.
This was referenced Aug 17, 2017
Member
Author
|
@ahejlsberg this undoes a change you added in the fix for #17521, but doesn't break any tests. I looked at the commit and the associated tests and the rest of the change looks like it handles types with index signatures, not mapped types. Regardless, you should take a look and then we can talk about the right way to fix this bug (#17069) at the same time as #17521's fix. |
Contributor
|
Merged this by mistake, sorry! (immediately reverted.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #17069
Fixes #15371
T[K]now correctly producesnumberwhenK extends string, T extends Record<K, number>.T[K]no longer allows any type to be assigned to it whenT extends object, K extends keyof T.Previously both of these cases failed in
getConstraintOfIndexedAccessTypebecause both bases followedK's base constraint tostringand then incorrectly producedanyfor types (likeobject) with no string index signature. In (1), this produced an error in checkBinaryLikeExpression. In (2), this failed to produce an error incheckTypeRelatedTo`.The fix has some additional pieces.
In order to fix (1), not only does the index type parameter need to be kept around, the base constraint of
T extends Record<K, number>needs to beRecord<K, number>not{}so thatRecord<K, number>[K]producesnumber. So I changedcomputeBaseConstraintto only chaseT extends Record<K, number>back toRecord<K, number>, not all the way to{}.In order to fix (2), any index type whose base constraint is string, whether it came from
keyof Tor not, is going to (incorrectly) produceanywhen indexing a type that doesn't have a string index signature -- the test case specifically triesobject[string]. So I added an early exit togetConstraintOfIndexedAccessTypethat returnsundefinedin this case.