Skip to content

Parameter of a generic interface doesn't work when it's a generic index of an another interface #32365

@marcingajda

Description

@marcingajda

TypeScript Version: typescript@next, [email protected]

Search Terms:
generic index is not assignable

Code

interface SettingsTypes {
  audio: {
    volume: string;
  };
  video: {
    resulution: string;
  }
}

interface Settings<Params extends { [K in keyof Params]?: string }> {
  config: Params;
};

// Example 1
type ThisWorks = Settings<SettingsTypes['audio']>;

// Example 2
type ThisDoesntWork<T extends keyof SettingsTypes> = Settings<SettingsTypes[T]>;

// ERROR:
// Type '{ volume: string; }' is not assignable to type '{ [K in keyof SettingsTypes[T]]?: string | undefined; }'.

// oh, ok but then why this works?

// Example 3
type ThisWorksAgain = Settings<{ volume: string; }>;

Expected behavior:
Generic should work the same if we pass an specific index of an interface (1), a generic index of an interface (2) or when we pass the type directly (3)

Actual behavior:
Typescript throws an error only for the construction Settings<SettingsTypes[T]>.

Playground Link:
https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgMoTGUBzAzgFQE8AHCXZAbwChlk4BXAE2AHsAuSm25ANxYBt6AWwgdcYKDgDcXAL4zaPYIwjtO3ZFDL1BWFiDETpcqrKpVQkWIhTpMOXAB4ACnChwh5CAA9IIRuQUyADaANLIoMgA1hCELDDIru6eALoA-IaSINjIsgB86sgI+jDA2BxJHrgy8uYA9HXIAKLeHsT8KACMVGAkKPgAFsC4AOosUFHkALxoGFjZTnbzeESkuMEA5AzMLBspeTJUDc2tQu0oAEw9fciDwwAiLGTgYxOO+Mg+fgHRsfGz9gWqzIBRmSwcjnBQL663w+0OxyaACUkQB5JFsI6NYHIDZBPiCESZaS5DYRcggFhgOi4XBlEBwABGHWQYBYrJueJC4UiMTiCShKxhwTh6WJ2WQAB9kPR-BBSiAIIwpKSAHT1RosAYAGmQLCiyEZ9GpYAGEBAyAA7gNCKyhuRLeNJmkNSc2iyAMzXUi3e2vSYAQWwcEiYLmEPxAmEomQ4iy2BV+SkQA

Related Issues:
32017 and 31904

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions