ts-redux-actions
Advanced tools
Comparing version 1.0.0-beta4 to 1.0.0-beta5
@@ -1,7 +0,5 @@ | ||
export declare type ActionCreatorFunction<T extends string, P> = (() => { | ||
type: T; | ||
}) | ((...args: any[]) => { | ||
type: T; | ||
payload: P; | ||
meta?: {}; | ||
export declare type ActionCreatorFunction<TS extends string> = ((...args: any[]) => { | ||
type: TS; | ||
payload?: any; | ||
meta?: any; | ||
error?: boolean; | ||
@@ -12,5 +10,5 @@ }); | ||
}; | ||
export declare function createAction<T extends string>(typeString: T): TSActionCreator<(() => { | ||
type: T; | ||
}), T>; | ||
export declare function createAction<T extends string, TS extends T, P, AC extends ActionCreatorFunction<T, P>>(typeString: TS, creatorFunction?: (type: T) => AC): TSActionCreator<AC, T>; | ||
export declare function createAction<TS extends string, AC extends (() => { | ||
type: TS; | ||
})>(typeString: TS): TSActionCreator<AC, TS>; | ||
export declare function createAction<TS extends string, AC extends ActionCreatorFunction<TS>>(typeString: TS, creatorFunction: AC): TSActionCreator<AC, TS>; |
@@ -8,3 +8,3 @@ "use strict"; | ||
} | ||
var actionCreator = creatorFunction(typeString); | ||
var actionCreator = creatorFunction; | ||
actionCreator.type = typeString; | ||
@@ -11,0 +11,0 @@ return actionCreator; |
@@ -12,5 +12,3 @@ "use strict"; | ||
it('with payload', function () { | ||
var add = _1.createAction('ADD', function (type) { return function (amount) { | ||
return ({ type: type, payload: amount }); | ||
}; }); | ||
var add = _1.createAction('ADD', function (amount) { return ({ type: 'ADD', payload: amount }); }); | ||
expect(add(10)).toEqual({ type: 'ADD', payload: 10 }); | ||
@@ -20,9 +18,7 @@ expect(add.type).toBe('ADD'); | ||
it('with payload and meta', function () { | ||
var notify = _1.createAction('NOTIFY', function (type) { | ||
return function (username, message) { return ({ | ||
type: type, | ||
payload: { message: username + ": " + message }, | ||
meta: { username: username, message: message }, | ||
}); }; | ||
}); | ||
var notify = _1.createAction('NOTIFY', function (username, message) { return ({ | ||
type: 'NOTIFY', | ||
payload: { message: username + ": " + message }, | ||
meta: { username: username, message: message }, | ||
}); }); | ||
expect(notify('Piotr', 'Hello!')).toEqual({ | ||
@@ -36,5 +32,6 @@ type: 'NOTIFY', | ||
it('with payload and no params', function () { | ||
var showNotification = _1.createAction('SHOW_NOTIFICATION', function (type) { | ||
return function () { return ({ type: type, payload: 'default message' }); }; | ||
}); | ||
var showNotification = _1.createAction('SHOW_NOTIFICATION', function () { return ({ | ||
type: 'SHOW_NOTIFICATION', | ||
payload: 'default message', | ||
}); }); | ||
var result = showNotification(); | ||
@@ -48,5 +45,6 @@ expect(result).toEqual({ | ||
it('with payload and optional param', function () { | ||
var showNotification = _1.createAction('SHOW_NOTIFICATION', function (type) { | ||
return function (message) { return ({ type: type, payload: message }); }; | ||
}); | ||
var showNotification = _1.createAction('SHOW_NOTIFICATION', function (message) { return ({ | ||
type: 'SHOW_NOTIFICATION', | ||
payload: message, | ||
}); }); | ||
expect(showNotification()).toEqual({ | ||
@@ -59,5 +57,6 @@ type: 'SHOW_NOTIFICATION', | ||
it('with meta and no params', function () { | ||
var showError = _1.createAction('SHOW_ERROR', function (type) { | ||
return function () { return ({ type: type, meta: { type: 'error' } }); }; | ||
}); | ||
var showError = _1.createAction('SHOW_ERROR', function () { return ({ | ||
type: 'SHOW_ERROR', | ||
meta: { type: 'error' }, | ||
}); }); | ||
expect(showError()).toEqual({ | ||
@@ -70,5 +69,7 @@ type: 'SHOW_ERROR', | ||
it('with meta and optional param', function () { | ||
var showError = _1.createAction('SHOW_ERROR', function (type) { | ||
return function (message) { return ({ type: type, payload: message, meta: { type: 'error' } }); }; | ||
}); | ||
var showError = _1.createAction('SHOW_ERROR', function (message) { return ({ | ||
type: 'SHOW_ERROR', | ||
payload: message, | ||
meta: { type: 'error' }, | ||
}); }); | ||
expect(showError()).toEqual({ | ||
@@ -75,0 +76,0 @@ type: 'SHOW_ERROR', |
@@ -1,7 +0,5 @@ | ||
export declare type ActionCreatorFunction<T extends string, P> = (() => { | ||
type: T; | ||
}) | ((...args: any[]) => { | ||
type: T; | ||
payload: P; | ||
meta?: {}; | ||
export declare type ActionCreatorFunction<TS extends string> = ((...args: any[]) => { | ||
type: TS; | ||
payload?: any; | ||
meta?: any; | ||
error?: boolean; | ||
@@ -12,5 +10,5 @@ }); | ||
}; | ||
export declare function createAction<T extends string>(typeString: T): TSActionCreator<(() => { | ||
type: T; | ||
}), T>; | ||
export declare function createAction<T extends string, TS extends T, P, AC extends ActionCreatorFunction<T, P>>(typeString: TS, creatorFunction?: (type: T) => AC): TSActionCreator<AC, T>; | ||
export declare function createAction<TS extends string, AC extends (() => { | ||
type: TS; | ||
})>(typeString: TS): TSActionCreator<AC, TS>; | ||
export declare function createAction<TS extends string, AC extends ActionCreatorFunction<TS>>(typeString: TS, creatorFunction: AC): TSActionCreator<AC, TS>; |
@@ -6,3 +6,3 @@ export function createAction(typeString, creatorFunction) { | ||
} | ||
var actionCreator = creatorFunction(typeString); | ||
var actionCreator = creatorFunction; | ||
actionCreator.type = typeString; | ||
@@ -9,0 +9,0 @@ return actionCreator; |
@@ -10,5 +10,3 @@ import { createAction } from '.'; | ||
it('with payload', function () { | ||
var add = createAction('ADD', function (type) { return function (amount) { | ||
return ({ type: type, payload: amount }); | ||
}; }); | ||
var add = createAction('ADD', function (amount) { return ({ type: 'ADD', payload: amount }); }); | ||
expect(add(10)).toEqual({ type: 'ADD', payload: 10 }); | ||
@@ -18,9 +16,7 @@ expect(add.type).toBe('ADD'); | ||
it('with payload and meta', function () { | ||
var notify = createAction('NOTIFY', function (type) { | ||
return function (username, message) { return ({ | ||
type: type, | ||
payload: { message: username + ": " + message }, | ||
meta: { username: username, message: message }, | ||
}); }; | ||
}); | ||
var notify = createAction('NOTIFY', function (username, message) { return ({ | ||
type: 'NOTIFY', | ||
payload: { message: username + ": " + message }, | ||
meta: { username: username, message: message }, | ||
}); }); | ||
expect(notify('Piotr', 'Hello!')).toEqual({ | ||
@@ -34,5 +30,6 @@ type: 'NOTIFY', | ||
it('with payload and no params', function () { | ||
var showNotification = createAction('SHOW_NOTIFICATION', function (type) { | ||
return function () { return ({ type: type, payload: 'default message' }); }; | ||
}); | ||
var showNotification = createAction('SHOW_NOTIFICATION', function () { return ({ | ||
type: 'SHOW_NOTIFICATION', | ||
payload: 'default message', | ||
}); }); | ||
var result = showNotification(); | ||
@@ -46,5 +43,6 @@ expect(result).toEqual({ | ||
it('with payload and optional param', function () { | ||
var showNotification = createAction('SHOW_NOTIFICATION', function (type) { | ||
return function (message) { return ({ type: type, payload: message }); }; | ||
}); | ||
var showNotification = createAction('SHOW_NOTIFICATION', function (message) { return ({ | ||
type: 'SHOW_NOTIFICATION', | ||
payload: message, | ||
}); }); | ||
expect(showNotification()).toEqual({ | ||
@@ -57,5 +55,6 @@ type: 'SHOW_NOTIFICATION', | ||
it('with meta and no params', function () { | ||
var showError = createAction('SHOW_ERROR', function (type) { | ||
return function () { return ({ type: type, meta: { type: 'error' } }); }; | ||
}); | ||
var showError = createAction('SHOW_ERROR', function () { return ({ | ||
type: 'SHOW_ERROR', | ||
meta: { type: 'error' }, | ||
}); }); | ||
expect(showError()).toEqual({ | ||
@@ -68,5 +67,7 @@ type: 'SHOW_ERROR', | ||
it('with meta and optional param', function () { | ||
var showError = createAction('SHOW_ERROR', function (type) { | ||
return function (message) { return ({ type: type, payload: message, meta: { type: 'error' } }); }; | ||
}); | ||
var showError = createAction('SHOW_ERROR', function (message) { return ({ | ||
type: 'SHOW_ERROR', | ||
payload: message, | ||
meta: { type: 'error' }, | ||
}); }); | ||
expect(showError()).toEqual({ | ||
@@ -73,0 +74,0 @@ type: 'SHOW_ERROR', |
@@ -1,7 +0,5 @@ | ||
export declare type ActionCreatorFunction<T extends string, P> = (() => { | ||
type: T; | ||
}) | ((...args: any[]) => { | ||
type: T; | ||
payload: P; | ||
meta?: {}; | ||
export declare type ActionCreatorFunction<TS extends string> = ((...args: any[]) => { | ||
type: TS; | ||
payload?: any; | ||
meta?: any; | ||
error?: boolean; | ||
@@ -12,5 +10,5 @@ }); | ||
}; | ||
export declare function createAction<T extends string>(typeString: T): TSActionCreator<(() => { | ||
type: T; | ||
}), T>; | ||
export declare function createAction<T extends string, TS extends T, P, AC extends ActionCreatorFunction<T, P>>(typeString: TS, creatorFunction?: (type: T) => AC): TSActionCreator<AC, T>; | ||
export declare function createAction<TS extends string, AC extends (() => { | ||
type: TS; | ||
})>(typeString: TS): TSActionCreator<AC, TS>; | ||
export declare function createAction<TS extends string, AC extends ActionCreatorFunction<TS>>(typeString: TS, creatorFunction: AC): TSActionCreator<AC, TS>; |
@@ -8,3 +8,3 @@ "use strict"; | ||
} | ||
const actionCreator = creatorFunction(typeString); | ||
const actionCreator = creatorFunction; | ||
actionCreator.type = typeString; | ||
@@ -11,0 +11,0 @@ return actionCreator; |
@@ -12,3 +12,3 @@ "use strict"; | ||
it('with payload', () => { | ||
const add = _1.createAction('ADD', (type) => (amount) => ({ type, payload: amount })); | ||
const add = _1.createAction('ADD', (amount) => ({ type: 'ADD', payload: amount })); | ||
expect(add(10)).toEqual({ type: 'ADD', payload: 10 }); | ||
@@ -18,4 +18,4 @@ expect(add.type).toBe('ADD'); | ||
it('with payload and meta', () => { | ||
const notify = _1.createAction('NOTIFY', (type) => (username, message) => ({ | ||
type, | ||
const notify = _1.createAction('NOTIFY', (username, message) => ({ | ||
type: 'NOTIFY', | ||
payload: { message: `${username}: ${message}` }, | ||
@@ -32,3 +32,6 @@ meta: { username, message }, | ||
it('with payload and no params', () => { | ||
const showNotification = _1.createAction('SHOW_NOTIFICATION', (type) => () => ({ type, payload: 'default message' })); | ||
const showNotification = _1.createAction('SHOW_NOTIFICATION', () => ({ | ||
type: 'SHOW_NOTIFICATION', | ||
payload: 'default message', | ||
})); | ||
const result = showNotification(); | ||
@@ -42,3 +45,6 @@ expect(result).toEqual({ | ||
it('with payload and optional param', () => { | ||
const showNotification = _1.createAction('SHOW_NOTIFICATION', (type) => (message) => ({ type, payload: message })); | ||
const showNotification = _1.createAction('SHOW_NOTIFICATION', (message) => ({ | ||
type: 'SHOW_NOTIFICATION', | ||
payload: message, | ||
})); | ||
expect(showNotification()).toEqual({ | ||
@@ -51,3 +57,6 @@ type: 'SHOW_NOTIFICATION', | ||
it('with meta and no params', () => { | ||
const showError = _1.createAction('SHOW_ERROR', (type) => () => ({ type, meta: { type: 'error' } })); | ||
const showError = _1.createAction('SHOW_ERROR', () => ({ | ||
type: 'SHOW_ERROR', | ||
meta: { type: 'error' }, | ||
})); | ||
expect(showError()).toEqual({ | ||
@@ -60,3 +69,7 @@ type: 'SHOW_ERROR', | ||
it('with meta and optional param', () => { | ||
const showError = _1.createAction('SHOW_ERROR', (type) => (message) => ({ type, payload: message, meta: { type: 'error' } })); | ||
const showError = _1.createAction('SHOW_ERROR', (message) => ({ | ||
type: 'SHOW_ERROR', | ||
payload: message, | ||
meta: { type: 'error' }, | ||
})); | ||
expect(showError()).toEqual({ | ||
@@ -63,0 +76,0 @@ type: 'SHOW_ERROR', |
{ | ||
"name": "ts-redux-actions", | ||
"version": "1.0.0-beta4", | ||
"version": "1.0.0-beta5", | ||
"description": "Typed Redux Actions for TypeScript Projects", | ||
@@ -5,0 +5,0 @@ "author": "Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)", |
@@ -47,5 +47,8 @@ # TS Redux Actions | ||
As a bonus there is a convenient `type` static property on every action creator for common reducer switch case scenarios (working with narrowing of union types): | ||
As a bonus there is a convenient `type` static property on every action creator for common reducer switch case scenarios (can be used to narrow "Discriminated Union" type): | ||
```ts | ||
const increment = createAction('INCREMENT'); | ||
// const increment: (() => { | ||
// type: "INCREMENT"; | ||
// }) & { readonly type: "INCREMENT"; } << HERE | ||
@@ -81,5 +84,5 @@ switch (action.type) { | ||
// only what is expected, no nullables, with inferred literal type in type property! | ||
// const notify1: () => { | ||
// const notify1: (() => { | ||
// type: "NOTIFY"; | ||
// } | ||
// }) & { readonly type: "NOTIFY"; } | ||
``` | ||
@@ -103,5 +106,5 @@ | ||
// with ts-redux-actions | ||
const notify2 = createAction('NOTIFY', (type: 'NOTIFY') => | ||
const notify2 = createAction('NOTIFY', | ||
(username: string, message?: string) => ({ | ||
type, | ||
type: 'NOTIFY', | ||
payload: { message: `${username}: ${message}` }, | ||
@@ -111,6 +114,6 @@ }) | ||
// still all good! | ||
// const notify2: (username: string, message?: string | undefined) => { | ||
// const notify2: ((username: string, message?: string | undefined) => { | ||
// type: "NOTIFY"; | ||
// payload: { message: string; }; | ||
// } | ||
// }) & { readonly type: "NOTIFY"; } | ||
@@ -135,5 +138,5 @@ ``` | ||
// with ts-redux-actions | ||
const notify3 = createAction('NOTIFY', (type: 'NOTIFY') => | ||
const notify3 = createAction('NOTIFY', | ||
(username: string, message?: string) => ({ | ||
type, | ||
type: 'NOTIFY', | ||
payload: { message: `${username}: ${message}` }, | ||
@@ -145,7 +148,7 @@ meta: { username, message }, | ||
// inference working as expected and compiler will catch all those nasty bugs: | ||
// const: notify: (username: string, message?: string | undefined) => { | ||
// const: notify: ((username: string, message?: string | undefined) => { | ||
// type: "NOTIFY"; | ||
// payload: { message: string; }; | ||
// meta: { username: string; message: string | undefined; }; | ||
// } | ||
// }) & { readonly type: "NOTIFY"; } | ||
``` | ||
@@ -163,6 +166,8 @@ | ||
```ts | ||
createAction(type, creatorFunction?) | ||
type: string, | ||
creatorFunction: (type: T) => (...args: any[]) => { type: T, payload?: P, meta?: M } | ||
return: (...args: any[]) => { type: T, payload?: P, meta?: M } | ||
createAction(typeString, creatorFunction?) | ||
typeString: TS extends string, | ||
creatorFunction: (...args: any[]) => { type: TS, payload?: P, meta?: M, error?: boolean } | ||
return: ( | ||
(...args: any[]) => { type: TS, payload?: P, meta?: M, error?: boolean } | ||
) & { readonly type: TS } | ||
``` | ||
@@ -169,0 +174,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
468
215
41560