From 44a897ac5a3bf2a6a8527f555531166fc9a9e357 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 24 Oct 2019 11:17:57 -0400 Subject: [PATCH] all is converted! --- src/buildInitial/index.test-d.js | 0 src/buildMiddleware/index.ts | 15 ++++++++------- src/{test.js => test.ts} | 26 +++++++++++++------------- src/types.ts | 6 ++++-- src/updux.ts | 16 +++++++++++----- tsconfig.json | 8 ++++---- 6 files changed, 40 insertions(+), 31 deletions(-) delete mode 100644 src/buildInitial/index.test-d.js rename src/{test.js => test.ts} (82%) diff --git a/src/buildInitial/index.test-d.js b/src/buildInitial/index.test-d.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/buildMiddleware/index.ts b/src/buildMiddleware/index.ts index bb5d61d..241a74c 100644 --- a/src/buildMiddleware/index.ts +++ b/src/buildMiddleware/index.ts @@ -1,7 +1,7 @@ import fp from 'lodash/fp'; -import { Middleware } from 'redux'; -import { Dictionary, ActionCreator, Action } from '../types'; +import { Middleware, MiddlewareAPI, Dispatch } from 'redux'; +import { Dictionary, ActionCreator, Action, UpduxDispatch } from '../types'; const MiddlewareFor = (type: any, mw: Middleware ): Middleware => api => next => action => { if (type !== '*' && action.type !== type) return next(action); @@ -11,12 +11,13 @@ const MiddlewareFor = (type: any, mw: Middleware ): Middleware => api => next => type Next = (action: Action) => any; -function buildMiddleware( - effects : Dictionary= {}, +function buildMiddleware( + effects : Dictionary>= {}, actions : Dictionary= {}, - subMiddlewares :Middleware[] = [], -) { - return (api: any) => { + subMiddlewares :Middleware<{},S,UpduxDispatch>[] = [], +): Middleware<{},S,UpduxDispatch> + { + return (api: MiddlewareAPI) => { for (let type in actions) { api.dispatch[type] = (...args:any[]) => api.dispatch(((actions as any)[type] as any)(...args)); } diff --git a/src/test.js b/src/test.ts similarity index 82% rename from src/test.js rename to src/test.ts index 32fb945..4468d57 100644 --- a/src/test.js +++ b/src/test.ts @@ -5,7 +5,7 @@ test('actions from mutations', () => { actions: {foo, bar}, } = updux({ mutations: { - foo: () => x => x, + foo: () => (x:any) => x, }, }); @@ -24,11 +24,11 @@ test('reducer', () => { const {actions, reducer} = updux({ initial: {counter: 1}, mutations: { - inc: () => ({counter}) => ({counter: counter + 1}), + inc: () => ({counter}:{counter:number}) => ({counter: counter + 1}), }, }); - let state = reducer(null, {}); + let state = reducer(null, {type:'noop'}); expect(state).toEqual({counter: 1}); @@ -41,16 +41,16 @@ test( 'sub reducers', () => { const foo = updux({ initial: 1, mutations: { - doFoo: () => (x) => x + 1, - doAll: () => x => x + 10, + doFoo: () => (x:number) => x + 1, + doAll: () => (x:number) => x + 10, }, }); const bar = updux({ initial: 'a', mutations: { - doBar: () => x => x + 'a', - doAll: () => x => x + 'b', + doBar: () => (x:string) => x + 'a', + doAll: () => (x:string) => x + 'b', } }); @@ -64,7 +64,7 @@ test( 'sub reducers', () => { expect(Object.keys(actions)).toHaveLength(3); - let state = reducer(null,{}); + let state = reducer(null,{type:'noop'}); expect(state).toEqual({ foo: 1, bar: 'a' }); @@ -92,7 +92,7 @@ test('precedence between root and sub-reducers', () => { foo: { bar: 4 }, }, mutations: { - inc: () => state => { + inc: () => (state:any) => { return { ...state, surprise: state.foo.bar @@ -106,7 +106,7 @@ test('precedence between root and sub-reducers', () => { quux: 3, }, mutations: { - inc: () => state => ({...state, bar: state.bar + 1 }) + inc: () => (state:any) => ({...state, bar: state.bar + 1 }) }, }), } @@ -122,7 +122,7 @@ test('precedence between root and sub-reducers', () => { }); -function timeout(ms) { +function timeout(ms:number) { return new Promise(resolve => setTimeout(resolve, ms)); } @@ -133,8 +133,8 @@ test( 'middleware', async () => { } = updux({ initial: "", mutations: { - inc: (addition) => state => state + addition, - doEeet: () => state => { + inc: (addition:number) => (state:number) => state + addition, + doEeet: () => (state:number) => { return state + 'Z'; }, }, diff --git a/src/types.ts b/src/types.ts index ae28d61..db8308d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import { Middleware } from 'redux'; +import { Dispatch, Middleware } from 'redux'; export type Action = { type: string, @@ -14,6 +14,8 @@ export type ActionPayloadGenerator = (...args:any[]) => any; export type ActionCreator = (...args: any[] ) => Action; +export type UpduxDispatch = Dispatch & Dictionary; + export type UpduxConfig = Partial<{ initial: S, subduxes: {}, @@ -21,7 +23,7 @@ export type UpduxConfig = Partial<{ [ type: string ]: ActionPayloadGenerator }, mutations: any, - effects: Dictionary, + effects: Dictionary>, }>; export type Upreducer = (action:Action) => (state:S) => S; diff --git a/src/updux.ts b/src/updux.ts index f734082..5d506fb 100644 --- a/src/updux.ts +++ b/src/updux.ts @@ -6,9 +6,13 @@ import buildMutations from './buildMutations'; import buildCreateStore from './buildCreateStore'; import buildMiddleware from './buildMiddleware'; import buildUpreducer from './buildUpreducer'; -import { UpduxConfig, Dictionary, Action, ActionCreator, Mutation, Upreducer } from './types'; +import { UpduxConfig, Dictionary, Action, ActionCreator, Mutation, Upreducer, UpduxDispatch } from './types'; -import { Middleware } from 'redux'; +import { Middleware, Store } from 'redux'; + +type StoreWithDispatchActions Action }> = Store & { + dispatch: { [ type in keyof Actions ]: (...args:any) => void } +}; export class Updux { @@ -24,9 +28,9 @@ export class Updux { reducer: (state:S|undefined,action:Action) => S; - middleware: Middleware; + middleware: Middleware<{},S,UpduxDispatch>; - createStore: Function; + createStore: () => StoreWithDispatchActions; constructor(config: UpduxConfig) { @@ -63,7 +67,9 @@ export class Updux { Object.values(this.subduxes).map( sd => sd.middleware ) ); - this.createStore = buildCreateStore(this.reducer,this.initial,this.middleware,this.actions); + const actions = this.actions; + this.createStore = buildCreateStore(this.reducer,this.initial,this.middleware as Middleware,this.actions) as + () => StoreWithDispatchActions< S, typeof actions >; } } diff --git a/tsconfig.json b/tsconfig.json index 29bc38a..a26462a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,12 +8,12 @@ "target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [ "dom", "es2019" ], /* Specify library files to be included in the compilation. */ - "allowJs": true, /* Allow javascript files to be compiled. */ + "allowJs": false, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ - "declaration": false, /* Generates corresponding '.d.ts' file. */ - "declarationMap": false, /* Generates a sourcemap for each corresponding '.d.ts' file. */ - "sourceMap": false, /* Generates corresponding '.map' file. */ + "declaration": true, /* Generates corresponding '.d.ts' file. */ + "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./dist", /* Redirect output structure to the directory. */ "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */