microsoft / microsoft/TypeScript
Mapped types over subclassed arrays behave strangely
@ahejlsberg ci sta già lavorando.
Dal 26/7/2024.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Valutazione
Questa issue non è ancora stata valutata.