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 types doesn't map the key value for methods when mapping over a generic instantiated with an array #58694

Closed
nicolo-ribaudo opened this issue May 29, 2024 · 5 comments

Comments

@nicolo-ribaudo
Copy link

nicolo-ribaudo commented May 29, 2024

πŸ”Ž Search Terms

mapped type array tuple prototype object

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAyghgWwgaQiAzgIQK7Ewe3wBsI4A7ANTiOwnQB4AVAPigF4oBvKAbWSgCWZKAGs0+AGZRGAXQBcUAEaES5KAF8AUJpLAoANwCMC+ElQYceFaUrVaDGBGD10wAE5CA5s2Y8ARBL4bgCicADGABZ+MuxQ7rQA3JoA9MlQAHoA-DpOBgBMJogoaFi4BMQ2VDR09ACCbm5wIC7uXj7+gSHhUTEc8RBJqRnZQA

πŸ’» Code

type SameKeysButBooleanValues<T> = { [K in keyof T]: boolean }

let v1: SameKeysButBooleanValues<Set<string>>["forEach"] = true;
// ^?
let v2: SameKeysButBooleanValues<Array<string>>["forEach"] = true;
// ^?

πŸ™ Actual behavior

  • v1 has type boolean
  • v2 has type (callbackfn: (value: boolean, index: number, array: boolean[]) => void, thisArg?: any) => void

πŸ™‚ Expected behavior

v2 has type boolean, same as v1

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.

@nicolo-ribaudo
Copy link
Author

nicolo-ribaudo commented May 29, 2024

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

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAggTnAhiA0hEBnAQgV2FgewIBsJEA7ANUWJwgygF4oBvKAbRSgEtyoBrdAQBmsBMgA8GYHF4BzAHwBdAFxQARkVIUoAXwBQ+0sCgA3AExr4SVOmx5CJMlRp0M7AETCCcAKKIAYwALDyUmKBk6AG59AHpYqAA9AH4NLWd9IA

and

type NotT = Array<string>;
type SameKeysButBooleanValues<T> = { [K in keyof NotT]: boolean }

let v2: SameKeysButBooleanValues<Array<string>>["forEach"] = true;
// ^? boolean

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9sAKlAvFAggJ0wQxAHgGdhMBLAOwHMA+AbgChRIoBlHAWwgGkIRCAhAK7B+cOABsIOcgDUc4wREL5E1VFADeUANpcoFKAGtecAGawEiALoAuKACMxk6VAC+9epOBQAbgCY7Nk4ePiERJylZeUVlLFwCYjIqamptACJTOEwAURwAYwALNKt1EkUGAHoKqAA9AH56IA

@nicolo-ribaudo nicolo-ribaudo changed the title Mapped types doesn't map the key value for methods when mapping over an array Mapped types doesn't map the key value for methods when mapping over a generic instantiated with an array May 29, 2024
@jcalz
Copy link
Contributor

jcalz commented May 29, 2024

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.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-1.html#mapped-types-on-tuples-and-arrays

@nicolo-ribaudo
Copy link
Author

Then is the bug the behavior I see in #58694 (comment)?

@jcalz
Copy link
Contributor

jcalz commented May 29, 2024

#27995, yes.

@nicolo-ribaudo
Copy link
Author

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"]>;

@nicolo-ribaudo nicolo-ribaudo closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants