Adding typings for next-redux-wrapper

This commit is contained in:
Stephen Ierodiaconou
2017-07-19 10:06:19 +01:00
parent d2bf223394
commit 9daad0b851
5 changed files with 189 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
// Type definitions for next-redux-wrapper 1.3
// Project: https://github.com/kirill-konshin/next-redux-wrapper
// Definitions by: Steve <https://github.com/stevegeek>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="node" />
/*~ 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<any>;
}
type NextReduxWrappedComponent = React.Component & NextPageComponentMethods;
type NextStoreCreator<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps> = (
initialState: TInitialState,
options: nextReduxWrapper.StoreCreatorOptions<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps>
) => Store<TInitialState>;
declare function nextReduxWrapper<TInitialState = any, TStateProps = any, TDispatchProps = any, TOwnProps = any, TMergedProps = any>(
options: nextReduxWrapper.Options<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps>
): (Component: Component<TOwnProps & TMergedProps>) => NextReduxWrappedComponent;
declare function nextReduxWrapper<TInitialState = any, TStateProps = any, TDispatchProps = any, TOwnProps = any, TMergedProps = any>(
createStore: NextStoreCreator<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
mapStateToProps?: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps?: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps?: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
options?: ConnectOptions
): (Component: Component<TOwnProps & TMergedProps>) => NextReduxWrappedComponent;
declare namespace nextReduxWrapper {
export interface Options<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps> {
createStore: NextStoreCreator<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps>;
debug?: boolean;
storeKey?: string;
mapStateToProps?: MapStateToPropsParam<TStateProps, TOwnProps>;
mapDispatchToProps?: MapDispatchToPropsParam<TDispatchProps, TOwnProps>;
mergeProps?: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>;
connectOptions?: ConnectOptions;
}
export interface StoreCreatorOptions<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps> extends Options<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps> {
isServer: boolean;
req?: IncomingMessage;
}
export function setPromise(Promise: any): void;
export function setDebug(debug: boolean): void;
}
@@ -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<InitialState> => {
return createStore(reducer, initialState);
};
interface Props {
foo: string;
custom: string;
}
interface ReduxStore {
foo: string;
}
class Page extends React.Component<Props> {
static getInitialProps({store, isServer, pathname, query}: any) {
store.dispatch({type: 'FOO', payload: 'foo'});
return {custom: 'custom'};
}
render() {
return (
<div>
<div>Prop from Redux {this.props.foo}</div>
<div>Prop from getInitialProps {this.props.custom}</div>
</div>
);
}
}
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<InitialState>(makeStore, (state: ReduxStore) => ({foo: state.foo}))(Page);
const com4 = withRedux<InitialState, ConnectStateProps>(
makeStore,
(state: ReduxStore) => ({foo: state.foo, custom: 'hi'})
)(Page);
const com5 = withRedux<InitialState, ConnectStateProps, DispatchProps, OwnProps, MergedProps>(
makeStore,
(state: ReduxStore) => ({foo: state.foo, custom: 'hi'}),
null, (state: Props) => ({foo: state.foo, custom: 'hi'})
)(Page);
const com6 = withRedux<InitialState, ConnectStateProps, DispatchProps, OwnProps, MergedProps>(
(initialState: InitialState, options: StoreCreatorOptions<InitialState, ConnectStateProps, DispatchProps, OwnProps, MergedProps>) => {
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);
+5
View File
@@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}
+24
View File
@@ -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"
]
}
+9
View File
@@ -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
}
}