Skip to content

Commit

Permalink
fix: remove the function fields from the result type of mapped types
Browse files Browse the repository at this point in the history
  • Loading branch information
luis.cabo committed Mar 18, 2023
1 parent 936e0dc commit 56b5d0d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/intersection-type.helper.ts
Expand Up @@ -6,6 +6,7 @@ import {
inheritTransformationMetadata,
inheritValidationMetadata,
} from './type-helpers.utils';
import { RemoveFieldsWithType } from './types/remove-fields-with-type.type';

// https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
Expand All @@ -26,7 +27,10 @@ type ClassRefsToConstructors<T extends 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]>
RemoveFieldsWithType<
UnionToIntersection<ClassRefsToConstructors<T>[number]>,
Function
>
>;

export function IntersectionType<T extends Type[]>(...classRefs: T) {
Expand All @@ -47,5 +51,6 @@ export function IntersectionType<T extends Type[]>(...classRefs: T) {
Object.defineProperty(IntersectionClassType, 'name', {
value: `Intersection${intersectedNames}`,
});

return IntersectionClassType as Intersection<T>;
}
7 changes: 5 additions & 2 deletions lib/omit-type.helper.ts
Expand Up @@ -5,11 +5,12 @@ import {
inheritTransformationMetadata,
inheritValidationMetadata,
} from './type-helpers.utils';
import { RemoveFieldsWithType } from './types/remove-fields-with-type.type';

export function OmitType<T, K extends keyof T>(
classRef: Type<T>,
keys: readonly K[],
): MappedType<Omit<T, (typeof keys)[number]>> {
) {
const isInheritedPredicate = (propertyKey: string) =>
!keys.includes(propertyKey as K);

Expand All @@ -22,5 +23,7 @@ export function OmitType<T, K extends keyof T>(
inheritValidationMetadata(classRef, OmitClassType, isInheritedPredicate);
inheritTransformationMetadata(classRef, OmitClassType, isInheritedPredicate);

return OmitClassType as MappedType<Omit<T, (typeof keys)[number]>>;
return OmitClassType as MappedType<
RemoveFieldsWithType<Omit<T, (typeof keys)[number]>, Function>
>;
}
8 changes: 6 additions & 2 deletions lib/partial-type.helper.ts
Expand Up @@ -6,8 +6,9 @@ import {
inheritTransformationMetadata,
inheritValidationMetadata,
} from './type-helpers.utils';
import { RemoveFieldsWithType } from './types/remove-fields-with-type.type';

export function PartialType<T>(classRef: Type<T>): MappedType<Partial<T>> {
export function PartialType<T>(classRef: Type<T>) {
abstract class PartialClassType {
constructor() {
inheritPropertyInitializers(this, classRef);
Expand All @@ -26,5 +27,8 @@ export function PartialType<T>(classRef: Type<T>): MappedType<Partial<T>> {
Object.defineProperty(PartialClassType, 'name', {
value: `Partial${classRef.name}`,
});
return PartialClassType as MappedType<Partial<T>>;

return PartialClassType as MappedType<
RemoveFieldsWithType<Partial<T>, Function>
>;
}
7 changes: 5 additions & 2 deletions lib/pick-type.helper.ts
Expand Up @@ -5,11 +5,12 @@ import {
inheritTransformationMetadata,
inheritValidationMetadata,
} from './type-helpers.utils';
import { RemoveFieldsWithType } from './types/remove-fields-with-type.type';

export function PickType<T, K extends keyof T>(
classRef: Type<T>,
keys: readonly K[],
): MappedType<Pick<T, (typeof keys)[number]>> {
) {
const isInheritedPredicate = (propertyKey: string) =>
keys.includes(propertyKey as K);

Expand All @@ -21,5 +22,7 @@ export function PickType<T, K extends keyof T>(
inheritValidationMetadata(classRef, PickClassType, isInheritedPredicate);
inheritTransformationMetadata(classRef, PickClassType, isInheritedPredicate);

return PickClassType as MappedType<Pick<T, (typeof keys)[number]>>;
return PickClassType as MappedType<
RemoveFieldsWithType<Pick<T, (typeof keys)[number]>, Function>
>;
}

0 comments on commit 56b5d0d

Please sign in to comment.