diff --git a/types/next-redux-wrapper/index.d.ts b/types/next-redux-wrapper/index.d.ts new file mode 100644 index 0000000000..d86e446c2a --- /dev/null +++ b/types/next-redux-wrapper/index.d.ts @@ -0,0 +1,64 @@ +// Type definitions for next-redux-wrapper 1.3 +// Project: https://github.com/kirill-konshin/next-redux-wrapper +// Definitions by: Steve +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// +/*~ Note that ES6 modules cannot directly export callable functions. + *~ This file should be imported using the CommonJS-style: + *~ import x = require('next-redux-wrapper'); + *~ + *~ Refer to the documentation to understand common + *~ workarounds for this limitation of ES6 modules. + */ + +import * as React from 'react'; +import { IncomingMessage } from 'http'; +import { + Store, Component, + MapDispatchToPropsParam, MapStateToPropsParam, + MergeProps, Options as ConnectOptions +} from 'react-redux'; + +export = nextReduxWrapper; + +interface NextPageComponentMethods { + getInitialProps(props: any): Promise; +} +type NextReduxWrappedComponent = React.Component & NextPageComponentMethods; + +type NextStoreCreator = ( + initialState: TInitialState, + options: nextReduxWrapper.StoreCreatorOptions +) => Store; + +declare function nextReduxWrapper( + options: nextReduxWrapper.Options +): (Component: Component) => NextReduxWrappedComponent; +declare function nextReduxWrapper( + createStore: NextStoreCreator, + mapStateToProps?: MapStateToPropsParam, + mapDispatchToProps?: MapDispatchToPropsParam, + mergeProps?: MergeProps, + options?: ConnectOptions +): (Component: Component) => NextReduxWrappedComponent; + +declare namespace nextReduxWrapper { + interface Options { + createStore: NextStoreCreator; + debug?: boolean; + storeKey?: string; + mapStateToProps?: MapStateToPropsParam; + mapDispatchToProps?: MapDispatchToPropsParam; + mergeProps?: MergeProps; + connectOptions?: ConnectOptions; + } + interface StoreCreatorOptions extends Options { + isServer: boolean; + req?: IncomingMessage; + } + + function setPromise(Promise: any): void; + function setDebug(debug: boolean): void; +} diff --git a/types/next-redux-wrapper/next-redux-wrapper-tests.tsx b/types/next-redux-wrapper/next-redux-wrapper-tests.tsx new file mode 100644 index 0000000000..de911679d2 --- /dev/null +++ b/types/next-redux-wrapper/next-redux-wrapper-tests.tsx @@ -0,0 +1,89 @@ +import * as React from 'react'; +import withRedux = require('next-redux-wrapper'); +import { createStore, Reducer, Store, AnyAction } from 'redux'; +import { StoreCreatorOptions } from 'next-redux-wrapper'; + +interface InitialState { + foo: string; +} + +const reducer: Reducer = (state: InitialState = {foo: ''}, action: AnyAction): InitialState => { + switch (action.type) { + case 'FOO': + return {...state, foo: action.payload}; + default: + return state; + } +}; + +const makeStore = (initialState: InitialState): Store => { + return createStore(reducer, initialState); +}; + +interface Props { + foo: string; + custom: string; +} + +interface ReduxStore { + foo: string; +} + +class Page extends React.Component { + static getInitialProps({store, isServer, pathname, query}: any) { + store.dispatch({type: 'FOO', payload: 'foo'}); + return {custom: 'custom'}; + } + render() { + return ( +
+
Prop from Redux {this.props.foo}
+
Prop from getInitialProps {this.props.custom}
+
+ ); + } +} + +type ConnectStateProps = Props; +type DispatchProps = Props; +type OwnProps = Props; +type MergedProps = Props; + +// Test various typings +const com1 = withRedux(makeStore, (state: ReduxStore) => ({foo: state.foo}))(Page); + +const com2 = withRedux(makeStore, (state: ReduxStore) => ({foo: state.foo}))(Page); + +const com3 = withRedux(makeStore, (state: ReduxStore) => ({foo: state.foo}))(Page); + +const com4 = withRedux( + makeStore, + (state: ReduxStore) => ({foo: state.foo, custom: 'hi'}) +)(Page); + +const com5 = withRedux( + makeStore, + (state: ReduxStore) => ({foo: state.foo, custom: 'hi'}), + undefined, + (state: Props) => ({foo: state.foo, custom: 'hi'}) +)(Page); + +const com6 = withRedux( + (initialState: InitialState, options: StoreCreatorOptions) => { + if (options.isServer) { + const a = 1; + } + return createStore(reducer, initialState); + }, + (state: ReduxStore) => ({foo: state.foo, custom: 'hi'}), + undefined, + (state: Props) => ({foo: state.foo, custom: 'hi'}) +)(Page); + +const com7 = withRedux({ + createStore: makeStore, + mapStateToProps: (state: ReduxStore) => ({foo: state.foo}) +})(Page); + +withRedux.setPromise(Promise); +withRedux.setDebug(true); diff --git a/types/next-redux-wrapper/package.json b/types/next-redux-wrapper/package.json new file mode 100644 index 0000000000..e52256ea90 --- /dev/null +++ b/types/next-redux-wrapper/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "redux": "^3.6.0" + } +} \ No newline at end of file diff --git a/types/next-redux-wrapper/tsconfig.json b/types/next-redux-wrapper/tsconfig.json new file mode 100644 index 0000000000..75ef2fd85b --- /dev/null +++ b/types/next-redux-wrapper/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "jsx": "react", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "next-redux-wrapper-tests.tsx" + ] +} diff --git a/types/next-redux-wrapper/tslint.json b/types/next-redux-wrapper/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/next-redux-wrapper/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }