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

keyof loses known keys when remapping an object type with a symbol key to remove a string index signature #45825

Description

@jcalz

Bug Report

This is a sequel to #41966.

🔎 Search Terms

keyof, key remapping, index signature, symbol, known keys

🕗 Version & Regression Information

  • This is the behavior in every version I tried since key remapping was introduced in TS4.1

⏯ Playground Link

Playground link with relevant code

💻 Code

const sym = Symbol("")
type Orig = { [k: string]: any, str: any, [sym]: any }

type Okay = Exclude<keyof Orig, never>
// type Okay = string | number | typeof sym

type Remapped = { [K in keyof Orig as {} extends Record<K, any> ? never : K]: any }
/* type Remapped = {
    str: any;
    [sym]: any;
} */
// no string index signature, right?

type Oops = Exclude<keyof Remapped, never>
// type Oops = typeof sym <-- what happened to "str"?

🙁 Actual behavior

keyof Remapped is typeof sym.

🙂 Expected behavior

keyof Remapped should be typeof sym | "str" because Remapped is {str: any, [sym]: any}.


It looks like the bug reported in #41966 was fixed for most cases, but not when the object type being remapped has a symbol-valued key. Not sure why. I ran into this when answering this Stack Overflow question.

Activity

  1. andrewbranch commented on Sep 10, 2021

    @andrewbranch
    Member

    There’s a clue as to what’s going on if you change the string index signature to a symbol index signature—we lose the [sym] key instead of the "str" key 🤔

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

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions