Skip to content

Commit

Permalink
fix(type): union expansions in intersections
Browse files Browse the repository at this point in the history
ref #556
  • Loading branch information
marcj committed Mar 12, 2024
1 parent c2a413a commit 332b26e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/type/src/reflection/processor.ts
Expand Up @@ -1339,6 +1339,9 @@ export class Processor {
}

function handleUnion(a: Type, unionType: TypeUnion): Type {
if (a.kind === ReflectionKind.objectLiteral || a.kind === ReflectionKind.class) {
return unboxUnion({ kind: ReflectionKind.union, types: unionType.types.map(v => collapse(v, a)).filter(v => v.kind !== ReflectionKind.never) });
}
return unboxUnion({ kind: ReflectionKind.union, types: unionType.types.filter(v => isExtendable(v, a)) });
}

Expand Down
12 changes: 12 additions & 0 deletions packages/type/tests/type.spec.ts
Expand Up @@ -1206,6 +1206,18 @@ test('union and intersection filters', () => {
expect(stringifyResolvedType(typeOf<{ a: string } & { b: number }>())).toBe(`{\n a: string;\n b: number;\n}`);
});

test('union expansion intersection', () => {
type U = { a: string } | { b: number };
type I = U & { c: boolean };
expect(stringifyResolvedType(typeOf<I>())).toBe(`{
a: string;
c: boolean;
} | {
b: number;
c: boolean;
}`);
});

test('index access on any', () => {
{
type Map2<T> = { [K in keyof T]: T[K] };
Expand Down

0 comments on commit 332b26e

Please sign in to comment.