Skip to content

Commit

Permalink
fix(type): keep same shaped but different interface in a union.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Aug 31, 2023
1 parent d55ff22 commit 17a7df8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/type/src/reflection/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,8 @@ export class Processor {
}
case ReflectionOp.union: {
const types = this.popFrame() as Type[];
let t: Type = unboxUnion({ kind: ReflectionKind.union, types: flattenUnionTypes(types) });
const flattened = flattenUnionTypes(types);
let t: Type = unboxUnion({ kind: ReflectionKind.union, types: flattened });
if (this.isEnded()) t = assignResult(program.resultType, t);
if (t.kind === ReflectionKind.union) for (const member of t.types) member.parent = t;
this.pushType(t);
Expand Down
1 change: 1 addition & 0 deletions packages/type/src/reflection/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ export function isSameType(a: Type, b: Type, stack: StackEntry[] = []): boolean

try {
if (a.kind !== b.kind) return false;
if (a.typeName !== b.typeName) return false;
if (a.kind === ReflectionKind.infer || b.kind === ReflectionKind.infer) return false;

if (a.kind === ReflectionKind.literal) return a.literal === (b as TypeLiteral).literal;
Expand Down
1 change: 0 additions & 1 deletion packages/type/tests/advanced.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { typeOf } from '../src/reflection/reflection.js';
import { assertType, ReflectionKind, stringifyResolvedType, Type } from '../src/reflection/type.js';
import { serialize } from '../src/serializer-facade.js';
import { expectEqualType } from './utils.js';

test('array stack', () => {
type Pop<T extends unknown[]> = T extends [...infer U, unknown] ? U : never
type Push<T extends unknown[], U> = [...T, U]
Expand Down
20 changes: 20 additions & 0 deletions packages/type/tests/reflection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test, expect } from '@jest/globals';
import { typeOf } from '../src/reflection/reflection.js';
import { assertType, ReflectionKind } from '../src/reflection/type.js';

test('union empty interfaces', () => {
interface Dog {
}

interface Cat {
}

//If two objectLiterals have the same shape, they are filtered out by flattenUnionTypes + isTypeIncluded.
//we se type.typeName additional now, so they are not filtered out. If this breaks in the future,
//we need to find a different way to filter out the same shapes.
const type = typeOf<Cat | Dog>();
assertType(type, ReflectionKind.union);
assertType(type.types[0], ReflectionKind.objectLiteral);
assertType(type.types[1], ReflectionKind.objectLiteral);
expect(type.types).toHaveLength(2);
});

0 comments on commit 17a7df8

Please sign in to comment.