Skip to content

Commit 0b824d0

Browse files
CraigMacomberanthony-murphy-agent
authored andcommitted
Add a few UnionToIntersection tests (microsoft#25768)
## Description Add a few UnionToIntersection tests
1 parent 47a7c11 commit 0b824d0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/dds/tree/src/test/util/typeUtils.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License.
44
*/
55

6+
import { allowUnused } from "../../simple-tree/index.js";
67
import type {
78
areSafelyAssignable,
89
requireAssignableTo,
@@ -89,4 +90,25 @@ import type {
8990
areSafelyAssignable<UnionToIntersection<{ x: 1 | 2 } | { x: 2 | 3 }>, { x: 2 }>
9091
>;
9192
type _check4 = requireTrue<areSafelyAssignable<UnionToIntersection<1>, 1>>;
93+
94+
// Check that arrays are preserved
95+
allowUnused<
96+
requireTrue<areSafelyAssignable<UnionToIntersection<readonly [1 | 2]>, readonly [1 | 2]>>
97+
>();
98+
99+
// Check that intersections are preserved
100+
allowUnused<
101+
requireTrue<
102+
areSafelyAssignable<UnionToIntersection<{ a: 1 } & { b: 2 }>, { a: 1 } & { b: 2 }>
103+
>
104+
>();
105+
106+
// Check intersections of unions behave as if intersection was distributed over union
107+
{
108+
type intersectedUnion = ({ a: 1 } | { b: 2 }) & { foo: 1 };
109+
type converted = UnionToIntersection<intersectedUnion>;
110+
allowUnused<
111+
requireTrue<areSafelyAssignable<converted, { a: 1 } & { b: 2 } & { foo: 1 }>>
112+
>();
113+
}
92114
}

0 commit comments

Comments
 (0)