From fde795ee6998606b0791f936a25ee85921c6586a Mon Sep 17 00:00:00 2001 From: "Marc J. Schmidt" Date: Tue, 24 Oct 2023 00:19:05 +0200 Subject: [PATCH] fix(type): correctly check `X extends Date` and print validation errors with caused value. Previously `{x: string} extends Date` returned true which is wrong. --- packages/type/src/reflection/extends.ts | 6 ++++++ packages/type/src/validator.ts | 2 +- packages/type/tests/type.spec.ts | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/type/src/reflection/extends.ts b/packages/type/src/reflection/extends.ts index 5a6b29565..bd359f2ce 100644 --- a/packages/type/src/reflection/extends.ts +++ b/packages/type/src/reflection/extends.ts @@ -214,6 +214,12 @@ export function isExtendable(leftValue: AssignableType, rightValue: AssignableTy return true; } + if ((left.kind === ReflectionKind.class || left.kind === ReflectionKind.objectLiteral) && (right.kind === ReflectionKind.class && right.classType === Date)) { + if (left.kind === ReflectionKind.objectLiteral && left.types.length === 0) return true; + if (left.kind === ReflectionKind.class && left.classType === Date) return true; + return false; + } + if ((left.kind === ReflectionKind.class || left.kind === ReflectionKind.objectLiteral) && (right.kind === ReflectionKind.objectLiteral || right.kind === ReflectionKind.class)) { const rightConstructor = (right.types as Type[]).find(v => (v.kind === ReflectionKind.methodSignature && v.name === 'new')) as TypeMethodSignature | undefined; diff --git a/packages/type/src/validator.ts b/packages/type/src/validator.ts index a88b16e36..1d429c8a1 100644 --- a/packages/type/src/validator.ts +++ b/packages/type/src/validator.ts @@ -99,7 +99,7 @@ export class ValidationErrorItem { messagedCausedBy = ` caused by value ${serialisedValue}`; } - return `${(prefix ? prefix + '.' : '') + this.path}(${this.code}): ${this.message}`; + return `${(prefix ? prefix + '.' : '') + this.path}(${this.code}): ${this.message}${messagedCausedBy}`; } } diff --git a/packages/type/tests/type.spec.ts b/packages/type/tests/type.spec.ts index d1892bb6c..780ec3d13 100644 --- a/packages/type/tests/type.spec.ts +++ b/packages/type/tests/type.spec.ts @@ -342,6 +342,11 @@ test('interface with method', () => { validExtend(); }); +test('extends Date', () => { + validExtend<{}, Date>(); + invalidExtend<{y: string}, Date>(); +}); + test('readonly constructor properties', () => { class Pilot { constructor(readonly name: string, readonly age: number) {