microsoft / microsoft/TypeScript
Mapped types over subclassed arrays behave strangely
@ahejlsberg is already working on this.
Since Jul 26, 2024.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
mapped type array subclass numeric constraint
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
class SubArray<T> extends Array<T> {
lastPushed: undefined | T;
}
type Test<T extends ReadonlyArray<any>> = {
[K in keyof T]: T[K];
};
type Result = Test<SubArray<number>>;
// ^?
type Push = Result["push"];
// ^?
🙁 Actual behavior
This mapped type doesn't have a homomorphic instantiation - its arrayness is not preserved. But yet all members are mapped to the Array's type argument
🙂 Expected behavior
I'm not entirely sure. On one hand, it feels that those members should be instantiated with the type of the original member. This wouldn't work with generic mapped types as T[K] has the numeric constraint when T has an array/tuple constraint. So the generic constraints wouldn't match what those could end up being instantiated with.
Should the result be a remapped subclassed array? That means that extra members wouldn't be mappable... but hey, regular array methods etc arent. So perhaps that's the most appropriate solution here.
cc @ahejlsberg
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.