Создание аргументов функции абстрактного класса из необязательных параметров абстрактного метода - PullRequest
0 голосов
/ 18 января 2020

Здесь у меня есть BaseRepository класс с этим readOneDirect методом. BaseRepository имеет этот необязательный метод readOneByRelation. OrganizationRepository расширяется от BaseRepository и реализует readOneByRelation.

Я хочу создать функцию в BaseRepository, которая является составной частью этого и других необязательных базовых методов.

Вот моя попытка:

type maybeProps2<T> = T extends (...args: any) => any ? Parameters<T>[0] : never

export interface BaseRepository<T> {
  readOneByRelation?(props: any): Promise<T | undefined | typeof NOT_RUN>
  readOneByUnique?(props: any): Promise<T | undefined | typeof NOT_RUN>
  readOneByJoin?(props: any): Promise<T | undefined | typeof NOT_RUN>
}

export abstract class BaseRepository<T> extends Repository<T> {

  async readOneDirect(_props: 
    maybeProps2<BaseRepository<T>['readOneByRelation']> | 
    maybeProps2<BaseRepository<T>['readOneByUnique']> |
    maybeProps2<BaseRepository<T>['readOneByJoin']>
  ) {
    const props: StrictUnion<typeof _props> = _props
    if (this.readOneByRelation) { 
      const entity = await this.readOneByRelation(props)
      if (entity !== NOT_RUN) return entity
    }
    if (this.readOneByUnique) { 
      const entity = await this.readOneByUnique(props)
      if (entity !== NOT_RUN) return entity
    }
    if (this.readOneByJoin) { 
      const entity = await this.readOneByJoin(props)
      if (entity !== NOT_RUN) return entity
    }
    return NOT_RUN
  }

}

@EntityRepository(Organization)
export class OrganizationRepository extends BaseRepository<Organization> {
  name = Organization.name
  relations = []

  async readOneByRelation(_props: { plan: Plan }) { 
    const props: StrictUnion<typeof _props> = _props
    if (props.plan?.organization) return props.plan.organization
    return undefined
  }

}

const or = new OrganizationRepository()

or.readOneDirect({ })

По какой-то причине при наведении на readOneDirect _props any. Возможно ли, чтобы это было ожидаемое значение {plan: Plan}?

...