microsoft / microsoft/TypeScript

Record keys are inferred to be values when using `extends string` conditional in a generic type

Open
#53,343 2 comments 3 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Mar 23, 2023.

Bug Domain: check: Type Inference Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

inference generic extends record

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "extends"
⏯ Playground Link

Playground link with relevant code

💻 Code
type TestType<Keys extends string> = string extends Keys ? Record<Keys, string> : Record<Keys, string>

function inferHelper<Keys extends string>(data: TestType<Keys>) {
  return data;
}

export const a = inferHelper({
  // key1 is inferred to be value1 here
  key1: "value1"
})
🙁 Actual behavior

ts2345

Argument of type '{ key1: string; }' is not assignable to parameter of type 'Record<"value1", string>'.
  Object literal may only specify known properties, and 'key1' does not exist in type 'Record<"value1", string>'
🙂 Expected behavior

no error, and the record is inferred as Record<"key1", string>

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.