Skip to content

Mapped types doesn't map the key value for methods when mapping over a generic instantiated with an array #58694

Closed as not planned

Description

🔎 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.

Activity

  1. nicolo-ribaudo commented on May 29, 2024

    @nicolo-ribaudo
    ContributorAuthor

    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

  2. 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[/+] on May 29, 2024
  3. jcalz commented on May 29, 2024

    @jcalz
    Contributor

    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

  4. nicolo-ribaudo commented on May 29, 2024

    @nicolo-ribaudo
    ContributorAuthor

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

  5. jcalz commented on May 29, 2024

    @jcalz
    Contributor

    #27995, yes.

  6. nicolo-ribaudo commented on May 29, 2024

    @nicolo-ribaudo
    ContributorAuthor

    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"]>;
  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions