Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inherit methods in type helprs #480

Closed
1 task done
mohsensaremi opened this issue Sep 28, 2021 · 1 comment
Closed
1 task done

inherit methods in type helprs #480

mohsensaremi opened this issue Sep 28, 2021 · 1 comment

Comments

@mohsensaremi
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

This feature is related to my issue that closed without any solution.

Previous issue:
#471

Describe the solution you'd like

We can create inheritMethods function like this:

export function inheritMethods(
  parentClass: Type<any>,
  targetClass: Function,
  isPropertyInherited?: (key: string) => boolean,
) {
  Object.getOwnPropertyNames(parentClass.prototype)
    .filter(
      (propertyName) =>
        !isPropertyInherited || isPropertyInherited(propertyName),
    )
    .forEach((name) => {
      Object.defineProperty(
        targetClass.prototype,
        name,
        Object.getOwnPropertyDescriptor(parentClass.prototype, name) ||
          Object.create(null),
      );
    });
}

and edit omit-type.helper.ts like this:

import { Type } from '@nestjs/common';
import { MappedType } from './mapped-type.interface';
import {
  inheritPropertyInitializers,
  inheritTransformationMetadata,
  inheritValidationMetadata,
  inheritMethods,
} from './type-helpers.utils';

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);

  abstract class OmitClassType {
    constructor() {
      inheritPropertyInitializers(this, classRef, isInheritedPredicate);
    }
  }

  inheritValidationMetadata(classRef, OmitClassType, isInheritedPredicate);
  inheritTransformationMetadata(classRef, OmitClassType, isInheritedPredicate);
  inheritMethods(classRef, OmitClassType, isInheritedPredicate);

  return OmitClassType as MappedType<Omit<T, typeof keys[number]>>;
}

and add inheritMethods(classRef, OmitClassType, isInheritedPredicate); too all type helpers.

Teachability, documentation, adoption, migration strategy

We just need to add this function in type-helpers.utils.ts

export function inheritMethods(
  parentClass: Type<any>,
  targetClass: Function,
  isPropertyInherited?: (key: string) => boolean,
) {
  Object.getOwnPropertyNames(parentClass.prototype)
    .filter(
      (propertyName) =>
        !isPropertyInherited || isPropertyInherited(propertyName),
    )
    .forEach((name) => {
      Object.defineProperty(
        targetClass.prototype,
        name,
        Object.getOwnPropertyDescriptor(parentClass.prototype, name) ||
          Object.create(null),
      );
    });
}

and use it in all type-helpers.

What is the motivation / use case for changing the behavior?

We can now inherit methods using type helpers

@kamilmysliwiec
Copy link
Member

This has been discussed several times in the past and we don't plan to add such a feature to this package.

@nestjs nestjs locked and limited conversation to collaborators Sep 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants