-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #871 from kpkonghk01/fix/intersect-more-than-4-cla…
…sses fix: IntersectionType intersects more than 4 classes
- Loading branch information
Showing
2 changed files
with
48 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,51 @@ | ||
import { Type } from '@nestjs/common'; | ||
|
||
import { MappedType } from './mapped-type.interface'; | ||
import { | ||
inheritPropertyInitializers, | ||
inheritTransformationMetadata, | ||
inheritValidationMetadata, | ||
} from './type-helpers.utils'; | ||
|
||
export function IntersectionType<A, B>( | ||
target: Type<A>, | ||
source: Type<B>, | ||
): MappedType<A & B>; | ||
|
||
export function IntersectionType<A, B, C>( | ||
target: Type<A>, | ||
sourceB: Type<B>, | ||
sourceC: Type<C>, | ||
): MappedType<A & B & C>; | ||
// https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type | ||
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ( | ||
k: infer I, | ||
) => void | ||
? I | ||
: never; | ||
|
||
export function IntersectionType<A, B, C, D>( | ||
target: Type<A>, | ||
sourceB: Type<B>, | ||
sourceC: Type<C>, | ||
sourceD: Type<D>, | ||
): MappedType<A & B & C & D>; | ||
// Converts ClassRefs array `Type<Class>[]` to `Class[]` using `infer` | ||
// e.g. `ClassRefsToConstructors<[Type<Foo>, Type<Bar>]>` becomes `[Foo, Bar]` | ||
type ClassRefsToConstructors<T extends Type[]> = { | ||
[U in keyof T]: T[U] extends Type<infer V> ? V : never; | ||
}; | ||
|
||
export function IntersectionType<A, T extends { new (...arg: any): any }[]>( | ||
classA: Type<A>, | ||
...classRefs: T | ||
): MappedType<A> { | ||
const allClassRefs = [classA, ...classRefs]; | ||
// Firstly, it uses indexed access type `Class[][number]` to convert `Class[]` to union type of it | ||
// e.g. `[Foo, Bar][number]` becomes `Foo | Bar` | ||
// then, uses the `UnionToIntersection` type to transform union type to intersection type | ||
// e.g. `Foo | Bar` becomes `Foo & Bar` | ||
// finally, returns `MappedType` passing the generated intersection type as a type argument | ||
type Intersection<T extends Type[]> = MappedType< | ||
UnionToIntersection<ClassRefsToConstructors<T>[number]> | ||
>; | ||
|
||
export function IntersectionType<T extends Type[]>(...classRefs: T) { | ||
abstract class IntersectionClassType { | ||
constructor() { | ||
allClassRefs.forEach((classRef) => { | ||
classRefs.forEach((classRef) => { | ||
inheritPropertyInitializers(this, classRef); | ||
}); | ||
} | ||
} | ||
|
||
allClassRefs.forEach((classRef) => { | ||
classRefs.forEach((classRef) => { | ||
inheritValidationMetadata(classRef, IntersectionClassType); | ||
inheritTransformationMetadata(classRef, IntersectionClassType); | ||
}); | ||
|
||
const intersectedNames = allClassRefs.reduce( | ||
(prev, ref) => prev + ref.name, | ||
'', | ||
); | ||
const intersectedNames = classRefs.reduce((prev, ref) => prev + ref.name, ''); | ||
Object.defineProperty(IntersectionClassType, 'name', { | ||
value: `Intersection${intersectedNames}`, | ||
}); | ||
return IntersectionClassType as MappedType<A>; | ||
return IntersectionClassType as Intersection<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters