Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

添加 typescript 支持 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {
Reducer,
Action,
AnyAction,
ReducersMapObject,
MiddlewareAPI,
StoreEnhancer,
bindActionCreators,
} from "redux";

import { History } from "history";

export interface Dispatch<A extends Action = AnyAction> {
<T extends A>(action: T): Promise<any> | T;
}

export interface onActionFunc {
(api: MiddlewareAPI<any>): void;
}

export interface ReducerEnhancer {
(reducer: Reducer<any>): void;
}

export interface Hooks {
onError?: (e: Error, dispatch: Dispatch<any>) => void;
onAction?: onActionFunc | onActionFunc[];
onStateChange?: () => void;
onReducer?: ReducerEnhancer;
onEffect?: () => void;
onHmr?: () => void;
extraReducers?: ReducersMapObject;
extraEnhancers?: StoreEnhancer<any>[];
}

export type DvaOption = Hooks & {
namespacePrefixWarning?: boolean;
initialState?: Object;
history?: Object;
};

export interface EffectsCommandMap {
put: <A extends AnyAction>(action: A) => any;
call: Function;
select: Function;
take: Function;
cancel: Function;
[key: string]: any;
}

export type Effect = (action: AnyAction, effects: EffectsCommandMap) => void;
export type EffectType = "takeEvery" | "takeLatest" | "watcher" | "throttle";
export type EffectWithType = [Effect, { type: EffectType }];
export type Subscription = (api: SubscriptionAPI, done: Function) => void;
export type ReducersMapObjectWithEnhancer = [
ReducersMapObject,
ReducerEnhancer
];

export interface EffectsMapObject {
[key: string]: Effect | EffectWithType;
}

export interface SubscriptionAPI {
history: History;
dispatch: Dispatch<any>;
}

export interface SubscriptionsMapObject {
[key: string]: Subscription;
}

export interface Model {
namespace: string;
state?: any;
reducers?: ReducersMapObject | ReducersMapObjectWithEnhancer;
effects?: EffectsMapObject;
subscriptions?: SubscriptionsMapObject;
}

export interface DvaInstance {
/**
* Register an object of hooks on the application.
*
* @param hooks
*/
use: (hooks: Hooks) => void;

/**
* Register a model.
*
* @param model
*/
model: (model: Model) => void;

/**
* Unregister a model.
*
* @param namespace
*/
unmodel: (namespace: string) => void;

/**
* Start the application. Selector is optional. If no selector
* arguments, it will return a function that return JSX elements.
*
* @param selector
*/
start: (selector?: React.ReactNode, opts?: { forwardRef: boolean }) => any;
}

export default function dva(opts?: DvaOption): DvaInstance;

export { bindActionCreators };

export {
connect,
connectAdvanced,
useSelector,
useDispatch,
useStore,
shallowEqual,
} from "react-redux";
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Provider, connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual } from 'react-redux';
import { bindActionCreators } from 'redux';
import { utils, create, saga } from 'dva-core';
import { create, saga } from 'dva-core';

export default function(opts = {}) {
const createOpts = {
Expand Down Expand Up @@ -34,8 +34,6 @@ export default function(opts = {}) {
return app;
}

function getProvider(store, app) {}

export { connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual };
export { bindActionCreators };
export { saga };