diff --git a/src/schematype.ts b/src/schematype.ts index 5983af7..0ba0b6d 100644 --- a/src/schematype.ts +++ b/src/schematype.ts @@ -64,7 +64,7 @@ class SchemaType { const default_ = this.options.default; if (typeof default_ === 'function') { - this.default = default_ as unknown as () => T; + this.default = default_ as () => T; } else { this.default = () => default_; } diff --git a/src/types/array.ts b/src/types/array.ts index 493dbce..dd27c7f 100644 --- a/src/types/array.ts +++ b/src/types/array.ts @@ -34,10 +34,10 @@ class SchemaTypeArray> extends SchemaType { * @return {Array} */ cast(value_: Exclude, data?: unknown): I[]; - cast(value_?: unknown, data?: unknown): I[] | null | undefined; - cast(value_?: unknown, data?: unknown): I[] | null | undefined { + cast(value_?: unknown, data?: unknown): I[] | undefined; + cast(value_?: unknown, data?: unknown): I[] | undefined { value_ = super.cast(value_, data); - if (value_ == null) return value_ as null | undefined; + if (value_ == null) return value_ as undefined; const value = isArray(value_) ? value_ : value_ = [value_]; if (!value.length) return value; diff --git a/src/types/buffer.ts b/src/types/buffer.ts index 7cb1da7..f5927ff 100644 --- a/src/types/buffer.ts +++ b/src/types/buffer.ts @@ -28,11 +28,11 @@ class SchemaTypeBuffer extends SchemaType { * @return {Buffer} */ cast(value_: WithImplicitCoercion | string>, data?: unknown): Buffer; - cast(value_?: unknown, data?: unknown): Buffer | null | undefined; - cast(value_?: unknown, data?: unknown): Buffer | null | undefined { + cast(value_?: unknown, data?: unknown): Buffer | undefined; + cast(value_?: unknown, data?: unknown): Buffer | undefined { const value = super.cast(value_, data); - if (value == null || Buffer.isBuffer(value)) return value as Buffer | null | undefined; + if (value == null || Buffer.isBuffer(value)) return value as Buffer | undefined; if (typeof value === 'string') return Buffer.from(value, this.options.encoding); if (Array.isArray(value)) return Buffer.from(value); } diff --git a/src/types/date.ts b/src/types/date.ts index 636e29b..398d09e 100644 --- a/src/types/date.ts +++ b/src/types/date.ts @@ -13,11 +13,11 @@ class SchemaTypeDate extends SchemaType { * @return {Date | null | undefined} */ cast(value_: Date | number | string): Date; - cast(value_?: unknown): Date | null | undefined; - cast(value_?: unknown): Date | null | undefined { + cast(value_?: unknown): Date | undefined; + cast(value_?: unknown): Date | undefined { const value = super.cast(value_, null); - if (value == null || value instanceof Date) return value as Date | null | undefined; + if (value == null || value instanceof Date) return value as Date | undefined; return new Date(value as any); } @@ -63,7 +63,7 @@ class SchemaTypeDate extends SchemaType { */ compare(a?: Date, b?: Date): number { if (a) { - return b ? (a as unknown as number) - (b as unknown as number) : 1; + return b ? a.getTime() - b.getTime() : 1; } return b ? -1 : 0; diff --git a/src/types/number.ts b/src/types/number.ts index fdae9c8..6b0a072 100644 --- a/src/types/number.ts +++ b/src/types/number.ts @@ -14,11 +14,11 @@ class SchemaTypeNumber extends SchemaType { * @return {Number} */ cast(value_: Exclude, data?: unknown): number; - cast(value_?: unknown, data?: unknown): number | null | undefined; - cast(value_?: unknown, data?: unknown): number | null | undefined { + cast(value_?: unknown, data?: unknown): number | undefined; + cast(value_?: unknown, data?: unknown): number | undefined { const value = super.cast(value_, data); - if (value == null || typeof value === 'number') return value as number | null | undefined; + if (value == null || typeof value === 'number') return value as number | undefined; return +value; } diff --git a/src/types/string.ts b/src/types/string.ts index f0c67bc..03d9be1 100644 --- a/src/types/string.ts +++ b/src/types/string.ts @@ -14,11 +14,11 @@ class SchemaTypeString extends SchemaType { * @return {String} */ cast(value_: { toString(): string }, data?: unknown): string; - cast(value_?: unknown, data?: unknown): string | null | undefined; - cast(value_?: unknown, data?: unknown): string | null | undefined { + cast(value_?: unknown, data?: unknown): string | undefined; + cast(value_?: unknown, data?: unknown): string | undefined { const value = super.cast(value_, data); - if (value == null || typeof value === 'string') return value as string | null | undefined; + if (value == null || typeof value === 'string') return value as string | undefined; if (typeof value.toString === 'function') return value.toString(); }