-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 types doesn't map the key value for methods when mapping over a generic instantiated with an array #58694
Comments
This only happens if I'm mapping over a generic type argument. If I inline it it works properly: type ArrayKeysButBooleanValues = { [K in keyof Array<string>]: boolean }
let v2: ArrayKeysButBooleanValues["forEach"] = true;
// ^? boolean and type NotT = Array<string>;
type SameKeysButBooleanValues<T> = { [K in keyof NotT]: boolean }
let v2: SameKeysButBooleanValues<Array<string>>["forEach"] = true;
// ^? boolean |
Generic mapped array/tuples are not supposed to map over all the keys. Itβs in the TS3.1 release notes. TS is behaving as intended; itβs not a bug. |
Then is the bug the behavior I see in #58694 (comment)? |
#27995, yes. |
Oh, closing as a duplicate of #27995 then :) Btw, my original use case was that I was trying to convert an array type into an array-like (i.e. same type, but without the array methods). I tried something like this but it obviously didn't work: type ToArrayLike<T extends unknown[]> = {
[K in keyof T]: K extends `${bigint}` ? T[K] : never
}
let v!: ToArrayLike<["a", "b", "c"]>; I then relaized that this is better: type ToArrayLike<T extends unknown[]> = {
[K in (keyof T) & `${bigint}`]: T[K]
}
let v!: ToArrayLike<["a", "b", "c"]>; |
π Search Terms
mapped type array tuple prototype object
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBAyghgWwgaQiAzgIQK7Ewe3wBsI4A7ANTiOwnQB4AVAPigF4oBvKAbWSgCWZKAGs0+AGZRGAXQBcUAEaES5KAF8AUJpLAoANwCMC+ElQYceFaUrVaDGBGD10wAE5CA5s2Y8ARBL4bgCicADGABZ+MuxQ7rQA3JoA9MlQAHoA-DpOBgBMJogoaFi4BMQ2VDR09ACCbm5wIC7uXj7+gSHhUTEc8RBJqRnZQA
π» Code
π Actual behavior
v1
has typeboolean
v2
has type(callbackfn: (value: boolean, index: number, array: boolean[]) => void, thisArg?: any) => void
π Expected behavior
v2
has typeboolean
, same asv1
Additional information about the issue
This looks closely related to #27995, but I believe that it is different. My issue is not about the set of keys being mapped, but about some of them not being mapped but just copied as-is from the original type.
The text was updated successfully, but these errors were encountered: