Skip to content

Cannot use valid union type as a map's key #27219

Closed
@Tyriar

Description

@Tyriar

TypeScript Version: 3.0

Search Terms:

map
union type
string | number

Code

const m: { [key: string | number]: any };

class C<K extends string | number> {
    private _m: { [key: K]: any };
}

Expected behavior:

It compiles.

Actual behavior:

An index signature parameter type must be 'string' or 'number'.
(parameter) key: string | number

Playground Link: http://www.typescriptlang.org/play/#src=const%20m%3A%20%7B%20%5Bkey%3A%20string%20%7C%20number%5D%3A%20any%20%7D%3B%0D%0A%0D%0Aclass%20C%3CK%20extends%20string%20%7C%20number%3E%20%7B%0D%0A%20%20%20%20private%20_m%3A%20%7B%20%5Bkey%3A%20K%5D%3A%20any%20%7D%3B%0D%0A%7D

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions