Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jcalz opened this issue Sep 10, 2021 · 1 comment · Fixed by #45923
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@jcalz
Copy link
Contributor

jcalz commented Sep 10, 2021

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.

@andrewbranch
Copy link
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 🤔

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Sep 10, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.1 milestone Sep 10, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants