microsoft / microsoft/TypeScript
Generic type changes depending if its parameter type is wrapped or not.
@weswigham arbeitet bereits daran.
Seit 05.12.2023.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.