使用Typescript初始化数据加载器时出现类型错误
我正在尝试使用以下代码初始化DataLoader的实例:使用Typescript初始化数据加载器时出现类型错误,typescript,graphql,dataloader,Typescript,Graphql,Dataloader,我正在尝试使用以下代码初始化DataLoader的实例: const authorLoader = new DataLoader(async (keys:string[]) => { // Return an author for each book }); 我得到以下错误: Argument of type '(keys: string[]) => Promise<Author[]>' is not assignable to parameter of typ
const authorLoader = new DataLoader(async (keys:string[]) => {
// Return an author for each book
});
我得到以下错误:
Argument of type '(keys: string[]) => Promise<Author[]>' is not assignable to parameter of type 'BatchLoadFn<string, Author>'.
Types of parameters 'keys' and 'keys' are incompatible.
The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'
“类型的参数”(key:string[])=>Promise”不可分配给“BatchLoadFn”类型的参数。
参数“键”和“键”的类型不兼容。
类型“readonly string[]”为“readonly”,无法分配给可变类型“string[]”
为什么会出现此错误?如何修复?我阅读了泛型和dataloader的源代码,但还没有找到解决方案
注意:keys
的类型是string[]
而不是number[]
,因为我使用的是uuid
。正如错误消息所说,数据加载器希望函数参数是只读字符串[]
,但您已经将其注释为string[]
const authorLoader = new DataLoader(async (keys: readonly string[]) => {
// Return an author for each book
});
哇,我很难为情地看到解决方案这么简单!谢谢,我们都去过!