From 9daad0b85191eaa2f612dec043d4f42b9fba43dc Mon Sep 17 00:00:00 2001 From: Stephen Ierodiaconou Date: Wed, 19 Jul 2017 10:06:19 +0100 Subject: [PATCH 1/3] Adding typings for next-redux-wrapper --- types/next-redux-wrapper/index.d.ts | 64 ++++++++++++++ .../next-redux-wrapper-tests.tsx | 87 +++++++++++++++++++ types/next-redux-wrapper/package.json | 5 ++ types/next-redux-wrapper/tsconfig.json | 24 +++++ types/next-redux-wrapper/tslint.json | 9 ++ 5 files changed, 189 insertions(+) create mode 100644 types/next-redux-wrapper/index.d.ts create mode 100644 types/next-redux-wrapper/next-redux-wrapper-tests.tsx create mode 100644 types/next-redux-wrapper/package.json create mode 100644 types/next-redux-wrapper/tsconfig.json create mode 100644 types/next-redux-wrapper/tslint.json diff --git a/types/next-redux-wrapper/index.d.ts b/types/next-redux-wrapper/index.d.ts new file mode 100644 index 0000000000..f65f3f9d73 --- /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 { + export interface Options { + createStore: NextStoreCreator; + debug?: boolean; + storeKey?: string; + mapStateToProps?: MapStateToPropsParam; + mapDispatchToProps?: MapDispatchToPropsParam; + mergeProps?: MergeProps; + connectOptions?: ConnectOptions; + } + export interface StoreCreatorOptions extends Options { + isServer: boolean; + req?: IncomingMessage; + } + + export function setPromise(Promise: any): void; + export 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..c432ae5ec9 --- /dev/null +++ b/types/next-redux-wrapper/next-redux-wrapper-tests.tsx @@ -0,0 +1,87 @@ +import * as React from 'react'; +import withRedux = require('next-redux-wrapper'); +import { createStore, Store, AnyAction } from 'redux'; +import { StoreCreatorOptions } from 'next-redux-wrapper'; + +interface InitialState { + foo: string; +} + +const reducer = (state: InitialState = {foo: ''}, action: AnyAction) => { + 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'}), + null, (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'}), + null, (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..f35fb6fa08 --- /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": false, + "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..98f9451050 --- /dev/null +++ b/types/next-redux-wrapper/tslint.json @@ -0,0 +1,9 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + // All of the different "export default" lines in the index.d.ts + // appear to be triggering this. Remove this when I know of a way + // to declare a default export across multiple package/subpackages. + "strict-export-declare-modifiers": false + } +} From e51079cdb2206f9efbc1a3e2879d2ca1938e480d Mon Sep 17 00:00:00 2001 From: Stephen Ierodiaconou Date: Wed, 19 Jul 2017 10:30:28 +0100 Subject: [PATCH 2/3] Lint fixed --- types/next-redux-wrapper/index.d.ts | 8 ++++---- types/next-redux-wrapper/next-redux-wrapper-tests.tsx | 10 ++++++---- types/next-redux-wrapper/tsconfig.json | 2 +- types/next-redux-wrapper/tslint.json | 10 +--------- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/types/next-redux-wrapper/index.d.ts b/types/next-redux-wrapper/index.d.ts index f65f3f9d73..e6c7ea600e 100644 --- a/types/next-redux-wrapper/index.d.ts +++ b/types/next-redux-wrapper/index.d.ts @@ -45,7 +45,7 @@ declare function nextReduxWrapper) => NextReduxWrappedComponent; declare namespace nextReduxWrapper { - export interface Options { + interface Options { createStore: NextStoreCreator; debug?: boolean; storeKey?: string; @@ -54,11 +54,11 @@ declare namespace nextReduxWrapper { mergeProps?: MergeProps; connectOptions?: ConnectOptions; } - export interface StoreCreatorOptions extends Options { + interface StoreCreatorOptions extends Options { isServer: boolean; req?: IncomingMessage; } - export function setPromise(Promise: any): void; - export function setDebug(debug: boolean): void; + 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 index c432ae5ec9..664d793826 100644 --- a/types/next-redux-wrapper/next-redux-wrapper-tests.tsx +++ b/types/next-redux-wrapper/next-redux-wrapper-tests.tsx @@ -1,13 +1,13 @@ import * as React from 'react'; import withRedux = require('next-redux-wrapper'); -import { createStore, Store, AnyAction } from 'redux'; +import { createStore, Reducer, Store, AnyAction } from 'redux'; import { StoreCreatorOptions } from 'next-redux-wrapper'; interface InitialState { foo: string; } -const reducer = (state: InitialState = {foo: ''}, action: AnyAction) => { +const reducer: Reducer = (state: InitialState = {foo: ''}, action: AnyAction): InitialState => { switch (action.type) { case 'FOO': return {...state, foo: action.payload}; @@ -64,7 +64,8 @@ const com4 = withRedux( const com5 = withRedux( makeStore, (state: ReduxStore) => ({foo: state.foo, custom: 'hi'}), - null, (state: Props) => ({foo: state.foo, custom: 'hi'}) + undefined, + (state: Props) => ({foo: state.foo, custom: 'hi'}) )(Page); const com6 = withRedux( @@ -75,7 +76,8 @@ const com6 = withRedux ({foo: state.foo, custom: 'hi'}), - null, (state: Props) => ({foo: state.foo, custom: 'hi'}) + undefined, + (state: Props) => ({foo: state.foo, custom: 'hi'}) )(Page); const com7 = withRedux({ diff --git a/types/next-redux-wrapper/tsconfig.json b/types/next-redux-wrapper/tsconfig.json index f35fb6fa08..75ef2fd85b 100644 --- a/types/next-redux-wrapper/tsconfig.json +++ b/types/next-redux-wrapper/tsconfig.json @@ -8,7 +8,7 @@ "jsx": "react", "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/next-redux-wrapper/tslint.json b/types/next-redux-wrapper/tslint.json index 98f9451050..3db14f85ea 100644 --- a/types/next-redux-wrapper/tslint.json +++ b/types/next-redux-wrapper/tslint.json @@ -1,9 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - // All of the different "export default" lines in the index.d.ts - // appear to be triggering this. Remove this when I know of a way - // to declare a default export across multiple package/subpackages. - "strict-export-declare-modifiers": false - } -} +{ "extends": "dtslint/dt.json" } From d7e97abc4fe2925e075d0638dbf69f2b9bd71d69 Mon Sep 17 00:00:00 2001 From: Stephen Ierodiaconou Date: Wed, 19 Jul 2017 10:34:00 +0100 Subject: [PATCH 3/3] Fix spacing --- types/next-redux-wrapper/index.d.ts | 4 ++-- types/next-redux-wrapper/next-redux-wrapper-tests.tsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/types/next-redux-wrapper/index.d.ts b/types/next-redux-wrapper/index.d.ts index e6c7ea600e..d86e446c2a 100644 --- a/types/next-redux-wrapper/index.d.ts +++ b/types/next-redux-wrapper/index.d.ts @@ -29,8 +29,8 @@ interface NextPageComponentMethods { type NextReduxWrappedComponent = React.Component & NextPageComponentMethods; type NextStoreCreator = ( - initialState: TInitialState, - options: nextReduxWrapper.StoreCreatorOptions + initialState: TInitialState, + options: nextReduxWrapper.StoreCreatorOptions ) => Store; declare function nextReduxWrapper( diff --git a/types/next-redux-wrapper/next-redux-wrapper-tests.tsx b/types/next-redux-wrapper/next-redux-wrapper-tests.tsx index 664d793826..de911679d2 100644 --- a/types/next-redux-wrapper/next-redux-wrapper-tests.tsx +++ b/types/next-redux-wrapper/next-redux-wrapper-tests.tsx @@ -81,8 +81,8 @@ const com6 = withRedux ({foo: state.foo}) + createStore: makeStore, + mapStateToProps: (state: ReduxStore) => ({foo: state.foo}) })(Page); withRedux.setPromise(Promise);