我有与ericgio对这个问题的回答中讨论的相同的问题(但从未回答(使用Typescript 进行React AsyncTypeahead
labelKey的类型定义似乎有问题
我从这个代码中得到以下错误:
<Typeahead
id="basic-typeahead-single"
labelKey="name"
onChange={this.changeEdgeName}
options={this.state.streetNames}
placeholder=""
/>
重载2(共2个(,"(props:Typeahead props,context?:any(:Typeahead",给出以下错误。类型"string"不可分配给类型"undefined"。TS2769
依赖项:
"@types/react-bootstrap-typeahead": "^3.4.6",
"react-bootstrap-typeahead": "^5.1.1",
我可以通过如下铸造来解决这个问题:
import { Typeahead, TypeaheadLabelKey } from 'react-bootstrap-typeahead';
...
labelKey={'displayName' as TypeaheadLabelKey<string>}
其中displayName
是您的labelKey。
我找到了一个解决方法!
我将标记分离到它自己的组件,在该组件中,由于某种原因不需要labelKey。
我不知道为什么这有效,但它对我来说有效!