与扩散构件打字稿接口
问题描述:
我批量导入一束性质的与与扩散构件打字稿接口
import * as actionCreators from './billingUtil2';
和打字原稿被正确识别的actionCreators
内的各出口。是否有可能将这些成员“传播”到一个界面中?理想的情况是这样的,但有效
interface componentState {
...actionCreators
}
我的使用情况是,我想创建一个组件作出反应,准确地描述它会从终极版将接收道具的形状。因此,理想的东西沿着这些线路
import * as actionCreators from './billingUtil2';
interface State {
name: string;
age: number
}
interface componentState extends State {
...actionCreators
}
,然后我可以告诉打字稿期望形式componentState
的道具。 我的redux reducer将已经返回实现接口的结果;我的主要目标是避免手动输入每个动作创建者。
答
您可以创建一个Intersection Type
import * as actionCreators from './billingUtil2';
type MyState = typeof actionCreators & {
name: string
age: number
}
或从第二部分上方,在这里你有State
接口代码,你可以做
import * as actionCreators from './billingUtil2';
interface State {
name: string;
age: number
}
type componentShape = typeof actionCreators & State;
,或者你可以同样做
type acT = typeof actionCreators
interface MyState extends acT {
name; age;
}
class Comp extends React.Component<{}, MyState> {
}