Skip to content

Commit

Permalink
chore: create a type to remove the fields with a certain type
Browse files Browse the repository at this point in the history
  • Loading branch information
luis.cabo committed Mar 18, 2023
1 parent 62bf680 commit 936e0dc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/omit-type.helper.ts
Expand Up @@ -9,7 +9,7 @@ import {
export function OmitType<T, K extends keyof T>(
classRef: Type<T>,
keys: readonly K[],
): MappedType<Omit<T, typeof keys[number]>> {
): MappedType<Omit<T, (typeof keys)[number]>> {
const isInheritedPredicate = (propertyKey: string) =>
!keys.includes(propertyKey as K);

Expand All @@ -22,5 +22,5 @@ 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<Omit<T, (typeof keys)[number]>>;
}
4 changes: 2 additions & 2 deletions lib/pick-type.helper.ts
Expand Up @@ -9,7 +9,7 @@ import {
export function PickType<T, K extends keyof T>(
classRef: Type<T>,
keys: readonly K[],
): MappedType<Pick<T, typeof keys[number]>> {
): MappedType<Pick<T, (typeof keys)[number]>> {
const isInheritedPredicate = (propertyKey: string) =>
keys.includes(propertyKey as K);

Expand All @@ -21,5 +21,5 @@ 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<Pick<T, (typeof keys)[number]>>;
}
8 changes: 8 additions & 0 deletions lib/types/remove-fields-with-type.type.ts
@@ -0,0 +1,8 @@
// With this util we could remove the keys with never type from the original type.
type KeysWithoutType<T, Type> = {
[K in keyof T]: T[K] extends Type ? never : K;
}[keyof T];

export type RemoveFieldsWithType<T, Type> = {
[K in KeysWithoutType<T, Type>]: T[K];
};

0 comments on commit 936e0dc

Please sign in to comment.