【发布时间】:2020-06-16 19:01:45
【问题描述】:
一旦我开始在React 中使用Typescript,我注意到我不喜欢的一件事是需要将每个道具声明给组件。在此之前我们可以使用{...props},但现在我必须在接口中声明每个原生props,例如ref、placeholder、defaultValue 等。
interface InputProps {
customProp: boolean;
props: any;
}
const Input = ({ customProp, placeholder, ...props }: InputProps) => {
//warning
return <input type="text" {...props} />;
};
https://codesandbox.io/s/distracted-burnell-vlt3i?file=/src/App.tsx
我想享受只需要在界面中声明非原生道具的旧日子,可能吗?原生 props 已通过 {...props}
【问题讨论】:
-
嗯.. 是的,这是意料之中的。那么您对此有何疑问?
-
@wentjun 更新了我的问题,见最后一行..
标签: javascript reactjs typescript