将 Typescript 与 Fuse.js 结合使用时出现此错误消息
TS2345: Argument of type '{ keys: string; }' is not assignable to parameter of type 'FuseOptions<{ 'ISBN': string; 'title': string; 'author': string; }>'. Types of property 'keys' are incompatible. Type 'string' is not assignable to type '("title" | "ISBN" | "author")[] | { name: "title" | "ISBN" | "author"; weight: number; }[]'.
代码如下:
let books = [{
'ISBN': 'A',
'title': "Old Man's War",
'author': 'John Scalzi'
},
{
'ISBN': 'B',
'title': 'The Lock Artist',
'author': 'Steve Hamilton'
}
]
let options = {
keys: 'title'
}
let fuse = new Fuse(books, options)
let filterResult = fuse.search('old')
错误是将鼠标悬停在 let fuse = new Fuse(books, options)
中的选项上时出现的错误
最佳答案
这也让我有点误会。
编译器按照从上到下的顺序移动,所以当它查看选项
时,推断它是一个string[]
,它不兼容您的图书类型的 keyof
。因此,在您的选项声明中添加一个类型注释。
另外,keys
应该是一个键数组。
最终结果:
type Book = { ISBN:string; title: string; author: string; };
let options: Fuse.FuseOptions<Book> = {
keys: ['title'],
};
关于reactjs - 使用 TypeScript react 和融合 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53610829/