我想为redux-form创建一些自定义输入字段。
所以我开始看一下文档中的Material UI example。
const renderTextField = ({input, label, meta: { touched, error }, ...custom }) =>
<TextField
hintText={label}
floatingLabelText={label}
errorText={touched && error}
{...input}
{...custom}
/>
看起来很直接,所以我查看了definitely typed type definitions for redux-form并找到了interfaces defining what I wanted。
interface WrappedFieldProps {
input: WrappedFieldInputProps;
meta: WrappedFieldMetaProps;
}
但他们没有出口?
那么,如果我无法访问界面,如何将界面应用于renderField
功能?
我错过了一些明显的东西,或采取了错误的方法吗?
我认为官方文档不对,导出时类型定义是否错误?
解决这个问题的一种方法是使渲染函数采用props:any
,但这会使类型定义无效!
const renderTextField = (props:any) => {
const {input, label, meta: { touched, error }, ...custom } = props;
return (
<TextField
hintText={label}
floatingLabelText={label}
errorText={touched && error}
{...input}
{...custom}
/>
);
}
因此,我不仅可以在自己的代码中使用接口,而且在将函数传递给<Field component={renderTextField} />
时也不会使用它们。
答案 0 :(得分:1)
我发现实际上可以导入接口,即使它们没有被导出!
我使用的是import * as ReduxForm from "redux-form";
,其名称为ReduxForm
。
查看ReduxForm
上可用的内容仅显示模块的导出部分。
但是,如果我使用明确要求非导出接口import { WrappedFieldProps } from "redux-form";
的导入,那么它可以工作,我可以使用该接口!
这让我感到困惑,它违背了我对导入/导出和模块工作方式的理解。但我很高兴我有一个解决方案。
我现在需要去阅读Typescript导入,以确定这是一个功能还是一个bug。 (我认为这是一个特色)。