Skip to content

Commit a3b170c

Browse files
committed
fix(types): defer type parameters in MutuallyAssignable and boolean types
as of TS4.9, conditional types are only deferred for single-element tuples, not multi-element ones. this should be fixed in microsoft/TypeScript#52091, slated for the TS5.0 milestone. until then, however, the implementations of `MutuallyAssignable`, `And`, and `Xor` can be assumed to be bugged. this commit fixes them to properly defer types. `Or` was also like this but fixed in earlier commit 0cd75d4
1 parent 7609a18 commit a3b170c

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

packages/types/src/bool/and.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
* checks if both `A` and `B` extend true
33
*/
44
export type And<A extends boolean, B extends boolean> =
5-
[A, B] extends [true, true]
6-
? true
5+
/* as of TS4.9, conditional types are only deferred for single-element tuples, not multi-element ones.
6+
* this should be fixed in https://github.com/microsoft/TypeScript/pull/52091/, slated for the TS5.0 milestone.
7+
*/
8+
// [A, B] extends [true, true]
9+
// ? true
10+
// : false
11+
[A] extends [true]
12+
? [B] extends [true]
13+
? true
14+
: false
715
: false

packages/types/src/bool/xor.d.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
* checks if one of `A` and `B` is true and the other false
33
*/
44
export type Xor<A extends boolean, B extends boolean> =
5-
[A, B] extends [true, false] ? true
6-
: [A, B] extends [false, true] ? true
5+
/* as of TS4.9, conditional types are only deferred for single-element tuples, not multi-element ones.
6+
* this should be fixed in https://github.com/microsoft/TypeScript/pull/52091/, slated for the TS5.0 milestone.
7+
* also relevant: https://github.com/microsoft/TypeScript/issues/51145#issuecomment-1276804047
8+
*/
9+
// [A, B] extends [true, false] ? true
10+
// : [A, B] extends [false, true] ? true
11+
// : false
12+
[A] extends [true]
13+
? [B] extends [false]
14+
? true
15+
: false
16+
: [A] extends [false]
17+
? [B] extends [true]
18+
? true
19+
: false
720
: false

packages/types/src/type/mutually-assignable.d.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
* for example, `[string, any]` is not `[any, string]` but both _are_ assignable to each other
77
*/
88
export type MutuallyAssignable<A, B> =
9-
[A, B] extends [B, A]
10-
? true
9+
/* as of TS4.9, conditional types are only deferred for single-element tuples, not multi-element ones.
10+
* this should be fixed in https://github.com/microsoft/TypeScript/pull/52091/, slated for the TS5.0 milestone.
11+
* also relevant: https://github.com/microsoft/TypeScript/issues/51145#issuecomment-1276804047
12+
*/
13+
// [A, B] extends [B, A]
14+
// ? true
15+
// : false
16+
[A] extends [B]
17+
? [B] extends [A]
18+
? true
19+
: false
1120
: false

0 commit comments

Comments
 (0)