microsoft / microsoft/TypeScript

KnownKeys<T> breaking change in 4.3.1-rc

Open
#44,143 9 comments 8 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since May 18, 2021.

Bug Domain: Conditional Types Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.