Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/type/src/reflection/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,8 @@ export class Processor {
return { kind: ReflectionKind.never };
}

if (b.kind === ReflectionKind.unknown) return a;

if (a.kind === ReflectionKind.objectLiteral || a.kind === ReflectionKind.class || a.kind === ReflectionKind.never || a.kind === ReflectionKind.unknown) return b;

if (b.annotations) {
Expand Down
25 changes: 25 additions & 0 deletions packages/type/tests/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ test('intersection with never', () => {
expect(groupAnnotation.getAnnotations(typeOf<C>())).toEqual(['c']);
});

test('intersection with unknown', () => {
// T & unknown = T (unknown is absorbed in intersections, per TypeScript semantics)
expect(stringifyType(typeOf<string & unknown>())).toBe('string');
expect(stringifyType(typeOf<unknown & string>())).toBe('string');
expect(stringifyType(typeOf<number & unknown>())).toBe('number');
expect(stringifyType(typeOf<unknown & number>())).toBe('number');
expect(stringifyType(typeOf<boolean & unknown>())).toBe('boolean');
expect(stringifyType(typeOf<bigint & unknown>())).toBe('bigint');
expect(stringifyType(typeOf<null & unknown>())).toBe('null');
expect(stringifyType(typeOf<undefined & unknown>())).toBe('undefined');

// object literal & unknown = object literal
type MyObj = { a: string };
expect(stringifyType(typeOf<MyObj & unknown>())).toBe('MyObj');
expect(stringifyType(typeOf<unknown & MyObj>())).toBe('MyObj');

// class & unknown = class
class MyClass { name!: string; }
expect(stringifyType(typeOf<MyClass & unknown>())).toBe('MyClass');
expect(stringifyType(typeOf<unknown & MyClass>())).toBe('MyClass');

// unknown & unknown = unknown
expect(stringifyType(typeOf<unknown & unknown>())).toBe('unknown');
});

test('intersection same type keep annotation', () => {
type MyAnnotation = { __meta?: never & ['myAnnotation'] };
type Username = string & MyAnnotation;
Expand Down