镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

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

Activity

  1. jack-williams commented on Sep 19, 2018

    @jack-williams
    Collaborator

    Probably fixed by #26797

  2. typescript-bot commented on Dec 14, 2018

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    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