microsoft / microsoft/TypeScript

Generic conditional type prematurely resolves to one of the branches instead of deferring

Open
#57,650 6 comments 0 reactions 1 assignee View on GitHub

@ahejlsberg is already working on this.

Since Mar 6, 2024.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

generic, conditional, premature / eager / defer, inline,

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

Playground link

💻 Code
type IsNumber<T> = [T] extends [number] ? true : false
type IsFalse<T extends boolean> = false[] extends T[] ? true : false

type Direct = IsFalse<IsNumber<string>> extends true ? 0 : 1;
//   ^? type Direct = 0

type Shortcut<T> = IsFalse<IsNumber<T>> extends true ? 0 : 1;
type Indirect = Shortcut<string>;
//   ^? type Indirect = 1

🙁 Actual behavior

Direct and Indirect are different.

🙂 Expected behavior

Direct and Indirect should be the same because Direct is just an inlined version of Shortcut<string>.

Additional information about the issue

This feels like a similar issue to #39364, #30708, #30020 and maybe some others, but many of those have been resolved already and yet this one remains. Came from this Stack Overflow question.

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.