繁体   English   中英

react-bootstrap-typeahead labelKey 打字稿

[英]react-bootstrap-typeahead labelKey Typescript

我有与 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",

我找到了解决方法!

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

我不知道为什么这有效,但它对我有用!

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

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

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

其中displayName是您的 labelKey。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM