From 6ef280f9c74841fba68d9d157eb4918e5b55fc8d Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Mon, 20 Dec 2021 09:28:03 -0600 Subject: [PATCH] feat(core): adjust typings to support more custom type converter --- .../src/lib/create-mapper/apply-type-converters.util.ts | 4 ++-- packages/core/src/lib/create-mapper/create-mapper.ts | 6 +++--- packages/core/src/lib/types/core.ts | 5 ++++- packages/core/src/lib/types/utils.ts | 3 ++- packages/core/src/lib/utils/is-date-constructor.util.ts | 2 +- .../core/src/lib/utils/is-primitive-constructor.util.ts | 9 ++++++++- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/core/src/lib/create-mapper/apply-type-converters.util.ts b/packages/core/src/lib/create-mapper/apply-type-converters.util.ts index d179868b9..11c2093d8 100644 --- a/packages/core/src/lib/create-mapper/apply-type-converters.util.ts +++ b/packages/core/src/lib/create-mapper/apply-type-converters.util.ts @@ -20,9 +20,9 @@ import { */ export function applyTypeConverters( mapping: Mapping, - typeConverters: WeakMap< + typeConverters: Map< PrimitiveConstructorExtended, - WeakMap + Map > ): void { const [, , initializedProps] = mapping; diff --git a/packages/core/src/lib/create-mapper/create-mapper.ts b/packages/core/src/lib/create-mapper/create-mapper.ts index 9b407b9a8..e85d2afc9 100644 --- a/packages/core/src/lib/create-mapper/create-mapper.ts +++ b/packages/core/src/lib/create-mapper/create-mapper.ts @@ -33,9 +33,9 @@ export function createMapper({ const plugin = pluginInitializer(errorHandler); // type converters - const typeConverters = new WeakMap< + const typeConverters = new Map< PrimitiveConstructorExtended, - WeakMap + Map >(); return { @@ -50,7 +50,7 @@ export function createMapper({ typeConverters.set( source, - new WeakMap([ + new Map([ [destination, converter], ]) ); diff --git a/packages/core/src/lib/types/core.ts b/packages/core/src/lib/types/core.ts index add55f2c9..dbbb7e20c 100644 --- a/packages/core/src/lib/types/core.ts +++ b/packages/core/src/lib/types/core.ts @@ -2,6 +2,7 @@ import { TransformationType } from './enums'; import type { CreateMapOptions, MapArrayOptions, MapOptions } from './options'; import type { Dictionary, + PrimitiveConstructor, PrimitiveConstructorExtended, PrimitiveExtended, Selector, @@ -161,7 +162,9 @@ export type MapInitializeReturn< export type PrimitiveConstructorReturnType< TType extends PrimitiveConstructorExtended -> = TType extends DateConstructor ? InstanceType : ReturnType; +> = TType extends DateConstructor | Exclude + ? InstanceType + : ReturnType>; export interface Mapper { name: string; diff --git a/packages/core/src/lib/types/utils.ts b/packages/core/src/lib/types/utils.ts index ae97e0c60..33770a695 100644 --- a/packages/core/src/lib/types/utils.ts +++ b/packages/core/src/lib/types/utils.ts @@ -21,7 +21,8 @@ export type PrimitiveConstructor = export type PrimitiveConstructorExtended = | PrimitiveConstructor - | DateConstructor; + | DateConstructor + | (new (...args: any[]) => any); export interface Selector< TObject extends Dictionary = any, diff --git a/packages/core/src/lib/utils/is-date-constructor.util.ts b/packages/core/src/lib/utils/is-date-constructor.util.ts index 9d1e56f89..b1413ca9d 100644 --- a/packages/core/src/lib/utils/is-date-constructor.util.ts +++ b/packages/core/src/lib/utils/is-date-constructor.util.ts @@ -4,5 +4,5 @@ * @param {Function} value */ export function isDateConstructor(value: unknown): boolean { - return value === Date; + return Object.getPrototypeOf(value) === Date || value === Date; } diff --git a/packages/core/src/lib/utils/is-primitive-constructor.util.ts b/packages/core/src/lib/utils/is-primitive-constructor.util.ts index 431c5f8a4..3e4789f6c 100644 --- a/packages/core/src/lib/utils/is-primitive-constructor.util.ts +++ b/packages/core/src/lib/utils/is-primitive-constructor.util.ts @@ -4,7 +4,14 @@ * @param {Function} value */ export function isPrimitiveConstructor(value: unknown): boolean { + const proto = Object.getPrototypeOf(value); return ( - value === String || value === Number || value === Boolean || value === Array + proto === String || + proto === Number || + proto === Boolean || + value === String || + value === Number || + value === Boolean || + value === Array ); }