Skip to content

An index signature parameter type must be 'string' or 'number' - why not string | number? #13398

@danielearwicker

Description

@danielearwicker

This:

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

specifies that the key must be either a string or a number, but produces an error saying that the key must be either string or number! But I guess this helps with clarity, ensuring that a value inserted with a string key "123" is not read with a number key 123 and vice versa.

But in the generic case:

interface A<K extends string|number> {
    [key: K]: boolean;
}

I could instantiate this with K = string|number, but more likely the intent is to pass one or the other, in which case this seems totally reasonable, but we currently can't do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions