Merge pull request #18208 from stevegeek/add_types_for_next_redux_wrapper

Adding new typings for `next-redux-wrapper`
This commit is contained in:
Wesley Wigham
2017-07-19 15:28:22 -07:00
committed by GitHub
5 changed files with 183 additions and 0 deletions

64
types/next-redux-wrapper/index.d.ts vendored Normal file
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 {
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;
}
interface StoreCreatorOptions<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps> extends Options<TInitialState, TStateProps, TDispatchProps, TOwnProps, TMergedProps> {
isServer: boolean;
req?: IncomingMessage;
}
function setPromise(Promise: any): void;
function setDebug(debug: boolean): void;
}

View File

@@ -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<InitialState> = (state: InitialState = {foo: ''}, action: AnyAction): InitialState => {
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'}),
undefined,
(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'}),
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);

View File

@@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }