react-bootstrap-typeahead labelKey typescript

IT技术 reactjs typescript twitter-bootstrap react-bootstrap react-bootstrap-typeahead
2021-05-13 00:51:41

我在 ericgio 对这个问题React AsyncTypeahead using Typescript的回答中讨论了(但从未回答)同样的问题

labelKey的类型定义似乎有问题

我从这段代码中得到以下错误:

<Typeahead
     id="basic-typeahead-single"
     labelKey="name"
     onChange={this.changeEdgeName}
     options={this.state.streetNames}
     placeholder=""
    />

重载 2 of 2, '(props: TypeaheadProps, context?: any): Typeahead',出现以下错误。类型 'string' 不可分配给类型 'undefined'。TS2769

依赖项:

"@types/react-bootstrap-typeahead": "^3.4.6",
    
"react-bootstrap-typeahead": "^5.1.1",

2个回答

我找到了解决方法!

我将标签与其自己的组件分开,在该组件中,出于某种原因不需要 labelKey。

我不知道为什么这有效,但对我来说确实如此!

我能够通过如下投射来解决这个问题:

import { Typeahead, TypeaheadLabelKey } from 'react-bootstrap-typeahead';
...

labelKey={'displayName' as TypeaheadLabelKey<string>}

displayName你的labelKey在哪里