首页 > 解决方案 > 打字稿编译错误 - TS2769:没有重载匹配此调用

问题描述

我已将 typescript 包更新到最新版本,现在在编译过程中遇到此错误:

TS2769: No overload matches this call

src/utils/search_module/index.ts:24:26 - error TS2769: No overload matches this call.


Overload 1 of 4, '(params?: Search<RequestBody<Record<string, any>>> | undefined, options?: TransportRequestOptions | undefined): TransportRequestPromise<...>', gave the following error.
    Type 'unknown' is not assignable to type 'string | Buffer | Readable | Record<string, any> | undefined'.
      Type 'unknown' is not assignable to type 'Record<string, any>'.
  Overload 2 of 4, '(callback: callbackFn<Record<string, any>, Context>): TransportRequestCallback', gave the following error.
    Argument of type '{ index: string; body: unknown; }' is not assignable to parameter of type 'callbackFn<Record<string, any>, Context>'.
      Object literal may only specify known properties, and 'index' does not exist in type 'callbackFn<Record<string, any>, Context>'.

24             return await client.search({
                            ~~~~~~~~~~~~~~~
25                 index: ELASTIC_SEARCH_INDEX,
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26                 body,
   ~~~~~~~~~~~~~~~~~~~~~
27             });
   ~~~~~~~~~~~~~~

这是代码:

import { Client } from '@elastic/elasticsearch';
import nr from 'newrelic';
import config from 'config';

import constants from '../../../config/constants';
import { Hit } from './types';

export { search };

const ELASTIC_SEARCH_HOST = config.get<string>(constants.ELASTIC_SEARCH_ENDPOINT);
const ELASTIC_SEARCH_INDEX = config.get<string>(constants.ELASTIC_SEARCH_INDEX);

const client = new Client({
    node: ELASTIC_SEARCH_HOST,
});

async function search<SourceResponse, HighlightResponse>(body: unknown): Promise<Hit<SourceResponse, HighlightResponse>> {
    const result = await nr.startSegment('search_module:search', true, async () => {
        return await client.search({
            index: ELASTIC_SEARCH_INDEX,
            body,
        });
    });
}

我查看了“@elastic/elasticsearch”的定义,但找不到解决此错误的方法。

请指教。

标签: node.jstypescript

解决方案


您的函数的body参数类型是. 但所需的类型是requestParams.d.tssearchunknown

export interface Search<T = RequestBody> extends Generic {
  //...
  body?: T;
}

所以该body属性具有RequestBody类型约束。

RequestBody类型:

导出类型 RequestBody<T = Record<string, any>> = T | 字符串 | 缓冲区 | 可读流

body不能unknown。它必须是Record<string, any>stringBufferReadableStream类型之一。

这就是你no overload signature matches your call出错的原因。


推荐阅读