reactjs - 使用 TypeScript react 和融合 js

标签 reactjs typescript fuse.js

将 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/

相关文章:

reactjs - react useMemo 内存清理

javascript - 类型 '{}' 缺少类型 'IResponseConfig' 的以下属性

javascript - 在 Django 上下文变量上使用 Javascript 搜索算法

javascript - fusionJS 模糊搜索对我不起作用

javascript - 如何只显示许多列表中的一些复选框项目?

javascript - Redux 在功能组件中分派(dispatch)后没有立即更新

javascript - ReactJS 状态重置自己

javascript - 我可以在 TypeScript 中将函数限制为纯函数吗?

ReactJS Material UI withStyles incl。 TS 中的主题

javascript - 限制fuse.js vue 中的搜索结果