Add next-redux-saga types (#29383)

* Add next-redux-saga types

* Add TypeScript version

* Matching TS version of React

* Sort by specific overload

* Fixes after npm run test

* Declare redux dependency

* Statically typed createStore

* For new packages, this file should only "extends": "dtslint/dt.json"

* require(next-redux-saga) is itself a function, not an object with a default property

* Enable ES Module Interop
This commit is contained in:
Leo Cavalcante
2018-10-08 15:39:17 -03:00
committed by Andy
parent 1899725695
commit 797efa7948
5 changed files with 112 additions and 0 deletions

12
types/next-redux-saga/index.d.ts vendored Normal file
View File

@@ -0,0 +1,12 @@
// Type definitions for next-redux-saga 3.0
// Project: https://github.com/bmealhouse/next-redux-saga
// Definitions by: Leo Cavalcante <https://github.com/leocavalcante>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import { ComponentType } from "react";
declare function nextReduxSaga<P = any>(arg: { async: boolean }): ((BaseComponent: ComponentType<P>) => ComponentType<P>);
declare function nextReduxSaga<P = any>(arg: ComponentType<P>): ComponentType<P>;
export = nextReduxSaga;

View File

@@ -0,0 +1,65 @@
import * as React from 'react';
import withRedux from 'next-redux-wrapper';
import withReduxSaga from 'next-redux-saga';
import { createStore, Reducer, Store, AnyAction } from 'redux';
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<InitialState>(reducer, initialState);
};
interface OwnProps {
bar: string;
}
interface Props {
foo: string;
custom: string;
}
class Page extends React.Component<OwnProps & 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 MergedProps = Props;
// Test various typings
const Com1 = withRedux<InitialState, ConnectStateProps, DispatchProps, OwnProps, MergedProps>(
(initialState: InitialState, options) => {
if (options.isServer || options.req || options.query || options.res) {
const a = 1;
}
return createStore(reducer, initialState);
},
)(withReduxSaga<OwnProps & Props>({ async: true })(Page));
const Com2 = withRedux(makeStore)(withReduxSaga(Page));
const com1Instance = (<Com1 bar="foo" />);
const com2Instance = (<Com2 />);

View File

@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"redux": "^3.6.0"
}
}

View File

@@ -0,0 +1,26 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"jsx": "react",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true
},
"files": [
"index.d.ts",
"next-redux-saga-tests.tsx"
]
}

View File

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