2017-06-05 39 views
1

我無法弄清楚爲什麼這個代碼構建失敗,出現以下錯誤:反應,Redux的connect()和錯誤TS2345

(43,3): error TS2345: Argument of type 'typeof Day' is not assignable to parameter of type 'Component<Props | (Props & ConnectProps & DispatchProps)>'. 
    Type 'typeof Day' is not assignable to type 'StatelessComponent<Props | (Props & ConnectProps & DispatchProps)>'. 
    Type 'typeof Day' provides no match for the signature '(props: (Props & { children?: ReactNode; }) | (Props & ConnectProps & DispatchProps & { children?: ReactNode; }), context?: any): ReactElement<any>'. 

我正在打字稿2.3.3和@類型/ react-redux的版本是4.4.41。

import * as React from "react"; 
import { connect } from "react-redux"; 

export interface Props { 
    date: Date; 
} 

export interface ConnectProps { 
    shifts: string[]; 
} 

export interface DispatchProps {} 

class Day extends React.Component<Props & ConnectProps & DispatchProps, null> { 
    render() { 
    /* simplified */ 
    return <div />; 
    } 
} 

const mapStateToProps = (state: any, ownProps?: Props): ConnectProps => { 
    /* simplified */ 
    return { 
    shifts: [] 
    }; 
}; 

const mapDispatchToProps = (dispatch: any): DispatchProps => ({}); 

export default connect<ConnectProps, DispatchProps, Props>(
    mapStateToProps, 
    mapDispatchToProps 
)(Day); 

let component = <Day date={new Date()} /> 

我想避免裝飾者,只要他們是實驗性的。

回答

1

I want to avoid decorators as long as they are experimental.

錯誤的方法來處理它。他們工作就像JSX工作。當前的類型定義假定你將使用裝飾器。

更多

如果你還不想使用裝飾你將不得不寫自己的類型定義,然後覆蓋從終極版的那些,這兩者都不是簡單的挑戰。歡迎您解決它們的樂趣,但需要更多的時間可以在有幫助的回答。

+0

請問你是如此友善,並分享一個如何使用@connect與mapstatetoprops,dispatchtoprops和一個commponent的類型安全定義的例子。我很努力。 – Karl2011