reactjs - 如何对包含对象的 props 接口进行 TypeScript 并与 defaultProps 合并
问题描述
我有一个像这样的 TypeScript 道具界面:
interface Props {
children: ReactNode;
components?: object;
mode: string;
actions: {
copy: boolean;
code: boolean;
refresh: boolean;
source: boolean;
};
source?: string;
readonly withStyles?: object;
styles?: object;
}
我有一个相应的 defaultProps 接口,如下所示:
class Box extends Component<Props, State> {
public static defaultProps = {
mode: 'full',
actions: {
copy: true,
code: true,
refresh: true,
source: true,
},
};
....
}
用户应该只能指定actions
组件的一部分,其余的应该从defaultProps
.
例如,如果用户指定:
<Box actions={{ copy: false }} />
我希望 defaultProps 将填补空白以生props.actions
成为{ copy: false, code: true, refresh: true, source: true }
.
但是,我目前收到一个 TypeScript 错误:Type '{ copy: boolean; }' is missing the following properties from type '{ copy: boolean; code: boolean; refresh: boolean; source: boolean; }': code, refresh, source
.
我怎样才能让它正常工作?
TypeScript v.3.2.2
@types/react v16.8.2
解决方案
React 没有深入的 defaultProps 检查/合并,TypeScript 不会改变这一点。
处理此问题的一种方法是制作包装器组件:
//pure js
const defaultActions = {
copy: true,
paste: false
};
const Box = props => JSON.stringify(props);
const BoxWithDefaultActions = ({ actions, ...rest }) => (
<Box actions={{ ...defaultActions, ...actions }} {...rest} />
);
const App = () => (
<>
<Box actions={{ copy: false }} />
<BoxWithDefaultActions actions={{ copy: false }} />
</>
);
/**
Box:
{ "actions": { "copy": false } }
Box with actions:
{ "actions": { "copy": false, "paste": false } }
**/
//HOC or render props component could be even better "solution".
您可以在 React Github 问题中阅读更多相关信息:https ://github.com/facebook/react/issues/2568
推荐阅读
- python - ipynb 将图像存储为什么格式?
- python - 遍历 URL 的 Dataframe 列并解析出 html 标签
- bash - 指定 --git-dir 或 -C 参数时 git checkout 自动完成
- c - 结构基地址递增给出不同的结果
- r - 在数据框中添加元素而不在 R 中循环的更好方法
- docker - 无法使用 Docker Swarm 连接到 Hyperledger 上的第二个节点的端点
- javascript - JavaScript 将图像读取为“未定义”
- discord.js - 运行此代码时不断收到此错误
- java - Java EE jsr352中的Spring批处理CompositeWriter等价物
- java - java 数组索引超出范围:第 17 行:java.lang.ArrayIndexOutOfBoundsException:索引 119 超出长度 26 的范围