0
因此,我正在做一個項目,我基本上編寫了自己的Redux版本,而我正在嘗試做一些非常複雜的打字工作,而且我沒有確定Flow是否有能力處理它。也許我做錯了什麼,但如果我能做到這一點,它會超級優雅。如何在流程中檢查類似Redux的連接()函數
這是我的connect
功能
// @flow
// TODO: think about how to make this more efficient, since forceUpdate on every subscribe will get inefficient
import React, { PureComponent } from 'react';
import type { Store } from './getStore';
function connectToStore<StateType, MappedProps: Object, PassedProps: Object>(
store: Store<StateType>,
mapStateToProps: (StateType) => MappedProps,
OldComponent: (PassedProps & MappedProps) => React$Element<*>
) {
return class Connected extends PureComponent<PassedProps, { storeState: StateType }> {
unsubscribe:() => void;
constructor(props: PassedProps, context: any) {
super(props, context);
this.state = {
storeState: store.getState()
};
this.unsubscribe = store.subscribe((state) => this.setState({ storeState: state }));
}
componentWillUnmount() {
this.unsubscribe();
}
render() {
const mappedProps = mapStateToProps(this.state.storeState);
return React.createElement(OldComponent, { ...this.props, ...mappedProps });
}
};
}
export default connectToStore;
那麼應該發生什麼的是,這部分的打字應該這麼說,如果我呼籲組分C此功能,那麼它會評估C和檢查的道具以確保通過與來自mapStateToProps的道具結合的組件的道具滿足C的每個道具要求。然而,發生什麼基本上根本沒有類型檢查。我不必填充C的任何prop支持要求(既不在mapToProps函數中,也不在從直接父級傳入的道具中),並且流程無論如何都接受它。這超出了流程的能力嗎?或者我錯過了什麼?