microsoft / microsoft/TypeScript
Generic type changes depending if its parameter type is wrapped or not.
@weswigham is already working on this.
Since Dec 5, 2023.
- 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
💻 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.