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

Fixed contextual types within generic tuple mapped types #53042

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

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Mar 1, 2023

It's a case that I came up with when reviewing #53036

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Mar 1, 2023
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@sandersn
Copy link
Member

sandersn commented Mar 9, 2023

This is the comment where you describe the bug, right?

@Andarist
Copy link
Contributor Author

Andarist commented Mar 9, 2023

Yep, it mentions this and related things

@@ -28959,6 +28959,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return isCircularMappedProperty(prop) ? undefined : getTypeOfSymbol(prop);
}
if (isTupleType(t) && isNumericLiteralName(name) && +name >= 0) {
if (t.target.hasRestElement) {
Copy link
Member

Choose a reason for hiding this comment

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

The tuple type could be [...T] where T extends [] and, thus, should not have a contextual type for a numbered element (at all), so I'm not sure checking this field is correct.

if (t.target.hasRestElement) {
const restElement = getTypeArguments(t)[t.target.fixedLength];
if (restElement !== type) {
const propType = getTypeOfPropertyOfContextualType(restElement, name);
Copy link
Member

Choose a reason for hiding this comment

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

Erm, name needs to be offset by the length of the preceding fixed elements of the tuple, no? And even that's thrown into question with multiple variadic tuple elements? Eg, if t is [...T, ...U] and name is 0, where T extends [] | [number] and U extends [] | [string], this is just going to lookup 0 on T, which is, at best, a partial result. (You'd want the contextual type to be something like string | number | undefined or something constrained to such). Seems like something a bit more complete is needed to handle variadic tuples fully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Waiting on author
Development

Successfully merging this pull request may close these issues.

5 participants