JCQ*_*tas 10 typescript reactjs
我是打字稿的新手,我正在尝试创建一个输入组件,如果它收到type="text"它呈现一个input,如果它收到type="textarea"它呈现,你得到它,一个textarea. 问题是,当我在代码上使用组件和 a 时,打字稿会抱怨onChange,似乎不允许我在同一个事件上使用两种类型?
它告诉我:
Type '(e: ChangeEvent<HTMLInputElement>) => void' is not assignable to type '(e?: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLTextAreaElement> | undefined) => void'.
Types of parameters 'e' and 'e' are incompatible.
Type 'ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLTextAreaElement> | undefined' is not assignable to type 'ChangeEvent<HTMLInputElement>'.
Type 'undefined' is not assignable to type 'ChangeEvent<HTMLInputElement>'.
Run Code Online (Sandbox Code Playgroud)
输入.js
interface InputProps {
className?: string;
type?: string;
placeholder?: string;
onChange?: (e?: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
}
const Input: FunctionComponent<InputProps> = ({ type = 'text', ...props }) => {
if (type === 'textarea') {
return <textarea {...props} />;
}
return <input type={type} {...props} />;
};
Run Code Online (Sandbox Code Playgroud)
用法:
class Usage extends React.Component<State> {
state: State;
onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ input: e.target.value });
};
render() {
return (
<Input placeholder="Write an something..." onChange={this.onInputChange} />
);
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
更新
我暂时解决它的方法是说 event 可以是 type any,但这有点像黑客
type CommonProps = {
className?: string;
placeholder?: string;
type?: string;
onChange?: (e: any) => void;
};
Run Code Online (Sandbox Code Playgroud)
您可以使用可区分联合来传递两种类型的参数,一种是 for text,另一种是for textarea。这具有确保处理程序和类型同步的额外好处。
type InputProps = { // The common Part
className?: string;
placeholder?: string;
} & ({ // The discriminated union
type?: "text";
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
} | {
type: "textarea";
onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
})
const Input: FunctionComponent<InputProps> = (props: InputProps) => {
if (props.type === 'textarea') {
return <textarea {...props} />;
}
return <input {...props} />;
};
class Usage extends React.Component<State> {
state: State;
onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ input: e.target.value });
};
render() {
return (
<Input placeholder="Write an something..." onChange={this.onInputChange} />
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10236 次 |
| 最近记录: |