microsoft / microsoft/TypeScript

Generic type changes depending if its parameter type is wrapped or not.

Open
#56,675 3 comments 1 reaction 1 assignee View on GitHub

@weswigham is already working on this.

Since Dec 5, 2023.

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

Description

🔎 Search Terms

generic type bug

🕗 Version & Regression Information

latest version as of today.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgZjAJhAdsAlgYwIYBsA8AGgDRQCapAglALxTABOArhKQEK1Rx4DOEAfLQBQUUVAAU+ACr9xASlqCpUCAA9gqRDyiEoAfigBGKAC4oAJgVqNKLSLGSZ8xVGXXN2svqOmLCg9RmbADcQmGgkLBwAPIM+AAKpACKpFLM0HSMLKQAYrwZXPmCdPBIqBg4BImFuHykpchoWHj4KTV1UHm1rK7p-Kl9oUIR0AD6xiUxcVk9M-zBUAD0i-Tpw+Bj5pzwsfgzpNzd80srM+uRowDM21P4hx1zC8urLOdjACw3u-c9P8fPPzCb1gPAAyox0CgAObSYpRMpNSrSUg8CHQ+bhDZQUYAVm2YLRMJQTAAtgAjCAMDFAkbYqQQVETKLffL1AkMSFE0kUqn-FY-AB0UEwAHsGAwIJhgABCIHAnYMADq6GAAAt4OCOdDYV84j82ZrObDqbTRvTUVtJrFlWqNYT8MTyZS+S8IAYyUwobKgA

💻 Code
type IfIdentical<X, Y, A = true, B = false> =
    (<T>() => T extends X ? 1 : 2) extends
    (<T>() => T extends Y ? 1 : 2) ? A : B;


type IfOr<P, Q, True = true, False = false> = IfIdentical<P, false, IfIdentical<Q, false, False, True>, True>;

type _1 = IfOr<true, true>; // true
type _2 = IfOr<true, false>; // true
type _3 = IfOr<false, true>; // true
type _4 = IfOr<false, false>; // false


type IsString<T> = IfIdentical<T, string>;

type _5 = IsString<number>;



type _Test1 = IfOr<false, IsString<number>>; // false. correct!



type IfOrWithIfString<T> = IfOr<false, IsString<T>>;

type _Test2 = IfOrWithIfString<number>; // true? bug!
🙁 Actual behavior

types _Test1 and _Test2 are different even though they are calculated the same way.

🙂 Expected behavior

types _Test1 and _Test2 should be the same.

Additional information about the issue

Not sure if it's the best minimal reproduction, because it's difficult to pinpoint the actual problem.
The example is based on the a type suggested by Matt McCutchen here, but I can't tell if the issue is unique to that type or if it happens in other scenarios.

Based on that IsEqual type I created a "if or" type IfOr<P, Q> and a "is string" type IsString<T>.

The difference between _Test1 and _Test2 is that the first passes IsString<number> result (false) directly to IfOr, while the second passes number to an intermediary type, sort of wrapping the IsString.

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.