node.js - 打字稿编译错误 - 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”的定义,但找不到解决此错误的方法。
请指教。
解决方案
您的函数的body
参数类型是. 但所需的类型是requestParams.d.tssearch
unknown
export interface Search<T = RequestBody> extends Generic {
//...
body?: T;
}
所以该body
属性具有RequestBody
类型约束。
RequestBody
类型:
导出类型 RequestBody<T = Record<string, any>> = T | 字符串 | 缓冲区 | 可读流
body
不能unknown
。它必须是Record<string, any>
、string
、Buffer
和ReadableStream
类型之一。
这就是你no overload signature matches your call
出错的原因。
推荐阅读
- python - 类的 AttributeError
- c# - 如何在其他游戏对象 Unity3D 的向前方向上移动游戏对象
- c# - 为 Visual Studio 社区设置默认模板语言(添加新项目对话框)
- unity3d - Unity快速方式网格对撞机到管道
- javascript - 正则表达式 || 如何获取“通配符”,特别是 replace() 函数
- javascript - 打字稿装饰器参数
- php - Codeigniter 从 Session 读取值或从 URL 参数读取值哪个更快?
- android - 所选项目上的 Android 微调器不起作用?
- c# - '列表
' 不包含 'Last' 的定义 - c - 为什么在特定行使用 getchar?