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

Mapped type applied to array type produces wrong toString, filter, etc type #57007

Closed
gabritto opened this issue Jan 10, 2024 · 3 comments Β· Fixed by #57093
Closed

Mapped type applied to array type produces wrong toString, filter, etc type #57007

gabritto opened this issue Jan 10, 2024 · 3 comments Β· Fixed by #57093
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Milestone

Comments

@gabritto
Copy link
Member

πŸ”Ž Search Terms

toString mapped type

πŸ•— Version & Regression Information

  • This changed between versions 4.6 and 4.7

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20240110#code/C4TwDgpgBAsghmMIA8AVKEAewIDsAmAzlAK64DWuA9gO64DaAugHxQC8UA3lPQNJQBLXFHIQQVAGZR0cYr0YAuaX0ZQAvgG4AsAChQkKADkqwAIIAnc3BDtYCJMlwkAtgCMI5ps207d+CADGADZw5tASZAHAAlTC+FQWViAAysAkEhIAFJhKZJS0DIwAlEoAblQC+D7+waHQAbGEwFA5RiaJ1tUJltap6VmYRRpAA

πŸ’» Code

type Mappy<T extends unknown[]> = { [K in keyof T as K]: T[K] };
type NotArray = Mappy<number[]>;

declare function doArrayStuff(x: unknown[]): void;
declare const x: NotArray;
doArrayStuff(x); // Error

πŸ™ Actual behavior

Error on doArrayStuff(x):
Argument of type 'Mappy<number[]>' is not assignable to parameter of type 'unknown[]'.
Types of property 'toString' are incompatible.
Type 'number' is not assignable to type '() => string'

πŸ™‚ Expected behavior

No error, type of x's toString is () => string.

Additional information about the issue

No response

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 10, 2024

This changed between versions 4.6 and 4.7

These say they were scheduled for 4.5, but I wonder if this is related to #46218 and #46169.

@gabritto
Copy link
Member Author

Bisected to #48837

@gabritto gabritto changed the title Mapped type applied to array type produces wrong toString type Mapped type applied to array type produces wrong toString, filter, etc type Jan 11, 2024
@gabritto
Copy link
Member Author

gabritto commented Jan 11, 2024

This happens because, as of #48837, whenever we see a mapped type like type Mappy<T extends unknown[]> = { [K in keyof T as K]: T[K] } where the modifier type (T) is an array or tuple, we add a numeric constraint to K in the template type. So the mapped type is internally represented as something like this:
type Mappy<T extends unknown[]> = { [K in keyof T as K]: T[K & (number | ${number})] }.
When we try to get the type of property toString of type Mappy<number[]>, what we do is instantiate the mapped type's template type, T[K & (number | ${number})], with T = number[] and K = "toString". So T[K & (number | ${number})] becomes (number[])["toString" & (number | ${number})], which resolves to (number[])[never]. Type number[] has an index signature mapping numbers to numbers, so (number[])[never] resolves to number via this index signature.
The same happens for the other array methods and they all end up having type number.

This problem is not present when you remove the as K clause in the mapped type, i.e. because then we resolve the mapped type Mappy<number[]> in a special way that directly creates an array type as a result, instead of the normal instantiation process described above.

#55774 would fix this.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 11, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 11, 2024
@gabritto gabritto self-assigned this Jan 19, 2024
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants