Skip to content

Multiple index signatures allowing different index typesΒ #44468

Closed
@ghost

Description

Suggestion

πŸ” Search Terms

Multiple more than one index type property signature type specific member mapping TypeScript TS

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Put further restrictions on index signatures being mapped to member types, driven by the type of the index signature. Basically add multiple index signatures to an interface that map different type keys to different type members. Hugely increases the power of index signatures. Blocked by #26797.

πŸ“ƒ Motivating Example

This:

export type ColorIdentifier =
    `${string}.${string}`
    | 'background'
    | 'contrastActiveBorder' | 'contrastBorder' | 'focusBorder'
    | 'descriptionForeground' | 'errorForeground' | 'foreground';
export type ThemeScopeKey = `[${string}]`;
export interface IThemeScopedColorCustomizations {
    [colorId: ColorIdentifier]: string;
}
export interface IColorCustomizations {
    [colorIdOrThemeScope: ColorIdentifier | ThemeScopeKey]: IThemeScopedColorCustomizations | string;
}

Could be more accurately specified as:

export type ColorIdentifier =
    `${string}.${string}`
    | 'background'
    | 'contrastActiveBorder' | 'contrastBorder' | 'focusBorder'
    | 'descriptionForeground' | 'errorForeground' | 'foreground';
export type ThemeScopeKey = `[${string}]`;
export interface IThemeScopedColorCustomizations {
    [colorId: ColorIdentifier]: string;
}
export interface IColorCustomizations {
    [colorId: ColorIdentifier]: string;
    [themeScope: ThemeScopeKey]: IThemeScopedColorCustomizations;
}

πŸ’» Use Cases

The vscode repo is full of interface collections that could make immediate use of this..

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