Skip to content
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

Contextually type array elements using index signatures #58288

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #58286

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Apr 22, 2024
@@ -30694,8 +30694,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
// 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(t, "" + index as __String) ||
getIteratedTypeOrElementType(IterationUse.Element, t, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false);
return (firstSpreadIndex === undefined || index < firstSpreadIndex) && getTypeOfPropertyOfContextualType(t, "" + index as __String) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 fixes here

  1. !firstSpreadIndex -> firstSpreadIndex === undefined. This fixes contextual parameter type here. It is not assigned correctly today. This PR makes it an implicit any which is better than the incorrect assignment.
  2. I also start using applicable index info as a fallback here. This covers cases when t is just an array-like that uses index signatures - that's not something that getIteratedTypeOrElementType handles today.

This comment was marked as spam.

@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels May 1, 2024
@sandersn sandersn added this to Not started in PR Backlog May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
PR Backlog
  
Not started
Development

Successfully merging this pull request may close these issues.

Type inference lost after spreading array with ArrayLike
3 participants