Skip to content

[Bug Report] Wrong keyof type with string index signature #39543

Closed

Description

@meteorlxy

TypeScript Version: 3.9.x

Search Terms: keyof, index signature

Code

interface Test1 {
    [key: string]: string;
}

type KeyofTest1 = keyof Test1

interface Test2 {
    [key: number]: string;
}

type KeyofTest2 = keyof Test2

Expected behavior:

  • KeyofTest1 should be string
  • KeyofTest2 should be number

Actual behavior:

  • KeyofTest1 is string | number (wrong)
  • KeyofTest2 is number (correct)

Playground Link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=12&pc=1#code/JYOwLgpgTgZghgYwgAgCoQM5gIzIN4BQyxyA2gNYQCeAXMllKAOYC6dDzA3AQL4EFgqABxQBpagHsY6LLgC8ySlSlpMOfqEixEKGWABM+IiQrU6IAK4BbAEbQ29MIxBNufAcLGTpawwqUqevoEQA

Related Issues:

Activity

  1. changed the title [-]Wrong keyof type with string index signature[/-] [+][Bug Report] Wrong keyof type with string index signature[/+] on Jul 10, 2020
  2. IllusionMH commented on Jul 10, 2020

    @IllusionMH
    Contributor

    This is by design
    https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#support-number-and-symbol-named-properties-with-keyof-and-mapped-types

    Given an object type X, keyof X is resolved as follows:

    If X contains a string index signature, keyof X is a union of string, number, and the literal types representing symbol-like properties, otherwise

  3. meteorlxy commented on Jul 10, 2020

    @meteorlxy
    Author

    Andrii Dieiev (@IllusionMH) Thanks for your quick reply. ❤️

    What's the underlay reason of this "by design" ? 🤔

    It looks odd and causes some strange type checking error in some third party packages

  4. IllusionMH commented on Jul 10, 2020

    @IllusionMH
    Contributor

    I think it's because there's no difference between obj[123] and obj["123"] in JS, and TS won't error in case of orders[orderId] if it's orderId is number (which would be annoying).
    Any number converted to string will be valid string, however not every string will be valid number therefore same is not applied for number index signature.

    I guess it should be OK to use Extract<keyof T, string> if you don't expect to use numbers there.

    But I can be wrong here.

  5. meteorlxy commented on Jul 10, 2020

    @meteorlxy
    Author

    Yeah a possible solution. I'll trying to submit a PR to that package.

    Thanks again~ 😄

  6. ExE-Boss commented on Nov 24, 2020

    @ExE-Boss
    Contributor

    Arguably, now that we have #40336 and #40598, keyof should report `${number}` for numeric index signatures.

  7. 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions