Как получить тип первой перегрузки в typescript?

Есть перегрузка вида
/**
   * Процедура получения области
   */
  Get(argument: GetAreaRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<GetAreaResponse__Output>): grpc.ClientUnaryCall;
  Get(argument: GetAreaRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<GetAreaResponse__Output>): grpc.ClientUnaryCall;
  Get(argument: GetAreaRequest, options: grpc.CallOptions, callback: grpc.requestCallback<GetAreaResponse__Output>): grpc.ClientUnaryCall;
  Get(argument: GetAreaRequest, callback: grpc.requestCallback<GetAreaResponse__Output>): grpc.ClientUnaryCall;


Нужно получить тип первой перегрузки
  • Вопрос задан
  • 129 просмотров
Пригласить эксперта
Ответы на вопрос 1
WblCHA
@WblCHA
https://github.com/microsoft/TypeScript/issues/321...

type OverloadProps<TOverload> = Pick<TOverload, keyof TOverload>;

type OverloadUnionRecursive<TOverload, TPartialOverload = unknown> = TOverload extends (
  ...args: infer TArgs
) => infer TReturn
  ? // Prevent infinite recursion by stopping recursion when TPartialOverload
    // has accumulated all of the TOverload signatures.
    TPartialOverload extends TOverload
    ? never
    :
        | OverloadUnionRecursive<
            TPartialOverload & TOverload,
            TPartialOverload & ((...args: TArgs) => TReturn) & OverloadProps<TOverload>
          >
        | ((...args: TArgs) => TReturn)
  : never;

type OverloadUnion<TOverload extends (...args: any[]) => any> = Exclude<
  OverloadUnionRecursive<
    // The "() => never" signature must be hoisted to the "front" of the
    // intersection, for two reasons: a) because recursion stops when it is
    // encountered, and b) it seems to prevent the collapse of subsequent
    // "compatible" signatures (eg. "() => void" into "(a?: 1) => void"),
    // which gives a direct conversion to a union.
    (() => never) & TOverload
  >,
  TOverload extends () => never ? never : () => never
>;

// Inferring a union of parameter tuples or return types is now possible.
type OverloadParameters<T extends (...args: any[]) => any> = Parameters<OverloadUnion<T>>;
type OverloadReturnType<T extends (...args: any[]) => any> = ReturnType<OverloadUnion<T>>;

https://www.typescriptlang.org/play?ts=4.7.2#code/...
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы