简体   繁体   English

如何在反应中获取输入字段类型作为道具 typescript

[英]How to get Input field type as props in react typescript

how I get input field type as the props in react typescript I tried to send type as the string but it gives this error我如何获得输入字段类型作为反应 typescript 中的道具我试图将类型作为字符串发送但它给出了这个错误

** **

No overload matches this call.没有过载匹配此调用。 Overload 1 of 2, '(props: InputProps | Readonly): Input', gave the following error.重载 1 of 2,'(props: InputProps | Readonly): Input',出现以下错误。 Type 'string' is not assignable to type '"number" |类型 'string' 不可分配给类型 '"number" | "button" | “按钮” | "select" | “选择” | "textarea" | “文本区域” | "time" | “时间” | "image" | “图像” | "text" | “文字” | "hidden" | “隐藏” | "color" | “颜色” | "email" | “电子邮件” | "file" | “文件” | "radio" | “收音机” | "checkbox" | “复选框” | "reset" | “重置” | "submit" | “提交” | "date" | “日期” | "datetime-local" |... 8 more... | "datetime-local" |... 8 更多... | undefined'.不明确的'。 Overload 2 of 2, '(props: InputProps, context: any): Input', gave the following error.重载 2 of 2,'(props: InputProps, context: any): Input',出现以下错误。 Type 'string' is not assignable to type '"number" |类型 'string' 不可分配给类型 '"number" | "button" | “按钮” | "select" | “选择” | "textarea" | “文本区域” | "time" | “时间” | "image" | “图像” | "text" | “文本” | "hidden" | “隐藏” | "color" | “颜色” | "email" | “电子邮件” | "file" | “文件” | "radio" | “收音机” | "checkbox" | “复选框” | "reset" | “重置” | "submit" | “提交” | "date" | “日期” | "datetime-local" |... 8 more... | "datetime-local" |... 8 更多... | undefined'.不明确的'。 TS2769 TS2769

** **

here is my code这是我的代码


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;

You could explicitly declare the type as:您可以将类型显式声明为:

import React, { ComponentProps } from 'react';
import { Input } from 'reactstrap';

interface IconInputProps {
    type: ComponentProps<typeof Input>['type'];
    // ...
}

This passes through the type declaration of a specific component prop and works even if that type is not exported by the given lib/component.会通过特定组件 prop 的类型声明,即使该类型未由给定的库/组件导出,它也能正常工作。


Though there are a few caveats:尽管有一些警告:

does not work with statically declared default props and generic props不适用于静态声明的默认道具和通用道具

Source: https://github.com/piotrwitek/react-redux-typescript-guide#reactcomponentpropstypeof-xxx资料来源: https://github.com/piotrwitek/react-redux-typescript-guide#reactcomponentpropstypeof-xxx

type: string should be replace by type: InputType type: string应替换为type: InputType

And don't forget to import this import { InputType } from "reactstrap/lib/Input.d";并且不要忘记import { InputType } from "reactstrap/lib/Input.d";

You can try to extend the InputProps which you should import from @types/reactstrap ( i guess it has types )您可以尝试扩展应该从@types/reactstrap InputProps我猜它有类型)

In your interface just add the props that are not in InputProps .在您的界面中,只需添加不在InputProps中的道具。 So you can remove type, name and so on.所以你可以删除type, name等。 So your code will look something like所以你的代码看起来像

interface IIconInputProps extends InputProps {
    label: string,
    errorMessage: string,
    icon: string
}

Also I would suggest to start the interface names with I so you know it's an interface.此外,我建议以I开头interface名称,这样您就知道它是一个接口。

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

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