Skip to content

Const indexed access issue β€” should attempt widening to a mapped typeΒ #51156

Description

Bug Report

Anders Hejlsberg (@ahejlsberg) This issue seems nearly identical to #47368, but this time for const object types which are assignable to the mapped type.

πŸ”Ž Search Terms

const indexed access; object literal indexed access; object literal widen; widen mapped type

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about indexed access.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type ArgMap = { a: number, b: string };
type Func<K extends keyof ArgMap> = (x: ArgMap[K]) => void;
type Funcs = { [K in keyof ArgMap]: Func<K> };

const funcs = {
    a: (x: number) => {},
    b: (x: string) => {},
};

function f1<K extends keyof ArgMap>(key: K, arg: ArgMap[K]) {
    funcs[key](arg);
}

function f2<K extends keyof ArgMap>(key: K, arg: ArgMap[K]) {
    const func = funcs[key];  // Type Funcs[K]
    func(arg);
}

function f3<K extends keyof ArgMap>(key: K, arg: ArgMap[K]) {
    const func: Func<K> = funcs[key];  // Error, Funcs[K] not assignable to Func<K>
    func(arg);
}

πŸ™ Actual behavior

Unexpected error in example above.

It's unexpected because throwing away type information makes this type check correctly, by widening with either const funcs: Funcs (all examples) or const func: Funcs[K] (in f2). So the compiler actually is able to confirm that indexed type is assignable to the mapped type, it just doesn't try without being explicitly prompted.

πŸ™‚ Expected behavior

No errors in example above.

Activity

  1. MartinJohns commented on Oct 12, 2022

    @MartinJohns
    Contributor

    This is working as intended.

    K can also be a union type, like "a" | "b". In this case your type Funcs[K] resolves to (x: number) => void | (x: string) => void. When trying to call a union like this, you must provide a value that's compatible with all possible argument types, because the compiler doesn't know whether it has a function accepting a string or a number - so the type you must provide must be both: string & number. But such a type can't be represented, so you end up with never.

    f1<"a" | "b">("a", "abc")
    f1<"a" | "b">("b", 123)
  2. RyanCavanaugh commented on Oct 12, 2022

    @RyanCavanaugh
    Member

    The error is correct, though if you formulate your arguments to a specific pattern, a convenient soundness hole will be created for you. See #47109

  3. typescript-bot commented on Oct 15, 2022

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions