gpt4 book ai didi

typescript - 使用 Typescript 初始化 DataLoader 时出现类型错误

转载 作者:行者123 更新时间:2023-12-05 02:07:11 26 4
gpt4 key购买 nike

我正在尝试使用以下代码初始化 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 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[]'

为什么会出现此错误以及如何解决?我阅读了 Generics 和 dataloader 的源代码,但没有找到解决方案。

注意:keysstring[] 类型而不是 number[] 因为我使用的是 uuid 的。

最佳答案

如错误消息所述,DataLoader 期望将 readonly string[] 作为函数参数,但您已将其注释为 string[]

const authorLoader = new DataLoader(async (keys: readonly string[]) => {
// Return an author for each book
});

关于typescript - 使用 Typescript 初始化 DataLoader 时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62052023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com