question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KnownKeys<T> breaking change in 4.3.1-rc

See original GitHub issue

Bug Report

🔎 Search Terms

KnownKeys, never, getting known interface keys

🕗 Version & Regression Information

  • This changed between versions 4.3.0-beta and 4.3.1-rc

⏯ Playground Link

💻 Code

type KnownKeys<T> = {
  [K in keyof T]: string extends K ? never : number extends K ? never : K
} extends { [_ in keyof T]: infer U } ? U : never;


interface HasStringKeys {
  [s: string]: any;
}

interface ThingWithKeys extends HasStringKeys {
  foo: unknown;
  bar: unknown;
}

const demo: KnownKeys<ThingWithKeys> = 'foo';

🙁 Actual behavior

demo has type never.

🙂 Expected behavior

demo has type 'foo' | 'bar'.

Other info

I’m not sure where I got KnownKeys from, but it appears on https://stackoverflow.com/a/51956054/123395.

This issue impacted https://www.npmjs.com/package/idb, but I’ve already worked around the issue by using the other method on that StackOverflow post. https://github.com/jakearchibald/idb/commit/e3c76a59f6ab461b03ceabebc24dea826a074a76

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jakearchibaldcommented, May 27, 2021

For others that run into this issue, the workaround was to replace KnownKeys with:

type RemoveIndex<T> = {
  [P in keyof T as string extends P
    ? never
    : number extends P
    ? never
    : P]: T[P];
};

type KnownKeys<T> = keyof RemoveIndex<T>;

Edit: I switched to @DanielRosenwasser’s version in https://github.com/microsoft/TypeScript/issues/44143#issuecomment-843450189, since it’s compatible with more TS versions. The workaround above caused compat issues https://github.com/jakearchibald/idb/issues/223

0reactions
weswighamcommented, Apr 6, 2022

The root cause is basically because string extends keyof T is true, so we fall into the never branch when calculating the implied constraint of infer U. We need to fix up that logic to, rather than instantiate the template to remove the K type parameter, resolve constraints in the template instead. A simple getBaseConstraintOfType instead of our complex instantiation logic may do nicely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found