import { expect, test } from '@jest/globals';
import { assertType, findMember, ReceiveType, ReflectionKind, resolveReceiveType, TypeClass, TypeObjectLiteral, typeOf } from '@deepkit/type';
import { nominalCompatibility } from '../src/types.js';
/**
* checks nominal compatibility of two types
*/
function compatible(token?: ReceiveType, provider?: ReceiveType): number {
return nominalCompatibility(resolveReceiveType(token), resolveReceiveType(provider) as TypeClass | TypeObjectLiteral);
}
test('nominal empty interface', () => {
interface A {
}
interface B {
}
interface B2 extends B {
}
interface C {
a: A;
}
const a = typeOf();
const c = typeOf();
assertType(a, ReflectionKind.objectLiteral);
assertType(c, ReflectionKind.objectLiteral);
const propA = findMember('a', c.types);
assertType(propA, ReflectionKind.propertySignature);
expect(compatible(propA.type, a)).toBe(1);
expect(compatible()).toBe(1);
expect(compatible()).toBe(0);
expect(compatible()).toBe(1);
expect(compatible()).toBe(2);
expect(compatible()).toBe(0);
});
test('nominal same interface', () => {
interface A {
id: string;
}
interface B {
id: string;
}
expect(compatible()).toBe(1);
expect(compatible()).toBe(0);
expect(compatible()).toBe(1);
expect(compatible()).toBe(0);
});
test('nominal extends interface', () => {
interface Base {}
interface Base2 {}
interface Connection extends Base {
id: number;
write(data: Uint16Array): void;
}
class MyConnection1 implements Connection, Base2 {
id: number = 0;
write(data: Uint16Array): void {
}
}
class MyConnection2 {
id: number = 0;
write(data: Uint16Array): void {
}
}
expect(compatible()).toBe(2);
expect(compatible()).toBe(0);
expect(compatible()).toBe(3);
expect(compatible()).toBe(2);
expect(compatible()).toBe(2);
expect(compatible()).toBe(0);
expect(compatible()).toBe(0);
expect(compatible()).toBe(0);
expect(compatible()).toBe(1);
expect(compatible()).toBe(1);
expect(compatible()).toBe(0);
expect(compatible()).toBe(0);
});