Skip to content

Support union type as index signature parameter type. #2611

Closed
@saml

Description

@saml

Given:

interface Foo {
  [key: string | number]: boolean;
}

I get

bleh.ts(2,4): error TS1023: An index signature parameter type must be 'string' or 'number'.

A workaround is:

interface Foo {
  [key: number]: boolean;
  [key: string]: boolean;
}

But, I need to match up the type of value, boolean. Since it's a repetition, it could be useful to programmers to specify index signature parameter type as string, number, string | number, or number | string.

Activity

danquirk

danquirk commented on Apr 3, 2015

@danquirk
Member

If your string and number indexers are the same then you don't need to do this. A string indexer subsumes the numeric indexer. If there's something more involved for indexer types you needed maybe #2049 is relevant.

added
By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead
on Apr 3, 2015
locked and limited conversation to collaborators on Jun 18, 2018
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

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @saml@danquirk

        Issue actions

          Support union type as index signature parameter type. · Issue #2611 · microsoft/TypeScript