【发布时间】:2021-02-23 06:32:46
【问题描述】:
我如何将输入字段类型作为反应打字稿中的道具我试图将类型作为字符串发送,但它给出了这个错误
**
没有重载匹配这个调用。 重载 1 of 2,'(props: InputProps | Readonly): Input',给出了以下错误。 类型“字符串”不可分配给类型“数字”| “按钮” | "选择" | “文本区域” | “时间” | “形象” | “文字” | “隐藏” | “颜色” | “电子邮件” | “文件” | “收音机” | “复选框” | “重置” | “提交” | “日期” | “本地日期时间” | ... 8 更多 ... |不明确的'。 重载 2 of 2,“(道具:InputProps,上下文:任何):输入”,给出了以下错误。 类型“字符串”不可分配给类型“数字”| “按钮” | "选择" | “文本区域” | “时间” | “形象” | “文字” | “隐藏” | “颜色” | “电子邮件” | “文件” | “收音机” | “复选框” | “重置” | “提交” | “日期” | “本地日期时间” | ... 8 更多 ... |不明确的'。 TS2769
**
这是我的代码
import React from 'react';
import { Input } from 'reactstrap';
interface IconInputProps {
name: string,
label: string,
placeholder: string,
required: boolean,
errorMessage: string,
autoFocus: boolean,
type: string
icon: string
}
class IconInput extends React.Component<IconInputProps> {
render() {
return (
<div>
<Input
name={this.props.name}
lable={this.props.label}
type={this.props.type}
placeholder={this.props.placeholder}
/>
</div>
);
}
}
export default IconInput;
【问题讨论】:
-
能提供这个组件的使用方法吗?
-
input(普通 HTML 元素)需要类型为字符串(我检查了代码和本地),但Input(您正在使用的来自 'reactstrap' 的那个需要更多具体类型)。 -
我猜,你还需要声明构造函数,就像reactjs.org/docs/uncontrolled-components.html#default-values中提到的那样
标签: javascript reactjs typescript