Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  41] [ 7]  / answers: 1 / hits: 12634  / 4 Years ago, wed, november 11, 2020, 12:00:00

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


**



No overload matches this call.
Overload 1 of 2, '(props: InputProps | Readonly): Input', gave the following error.
Type 'string' is not assignable to type '"number" | "button" | "select" | "textarea" | "time" | "image" | "text" | "hidden" | "color" | "email" | "file" | "radio" | "checkbox" | "reset" | "submit" | "date" | "datetime-local" | ... 8 more ... | undefined'.
Overload 2 of 2, '(props: InputProps, context: any): Input', gave the following error.
Type 'string' is not assignable to type '"number" | "button" | "select" | "textarea" | "time" | "image" | "text" | "hidden" | "color" | "email" | "file" | "radio" | "checkbox" | "reset" | "submit" | "date" | "datetime-local" | ... 8 more ... | undefined'. 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;


More From » reactjs

 Answers
10

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.




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


[#2324] Friday, November 6, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileent

Total Points: 556
Total Questions: 107
Total Answers: 101

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
;