immer-reducer
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -8,8 +8,9 @@ import { Draft } from "immer"; | ||
}[keyof T]; | ||
declare type MethodObject = { | ||
[key: string]: () => any; | ||
}; | ||
/** Pick only methods from object */ | ||
declare type Methods<T> = Pick<T, FunctionPropertyNames<T>>; | ||
/** flatten functions in an object to their return values */ | ||
declare type FlattenToReturnTypes<T extends { | ||
[key: string]: () => any; | ||
}> = { | ||
declare type FlattenToReturnTypes<T extends MethodObject> = { | ||
[K in keyof T]: ReturnType<T[K]>; | ||
@@ -20,5 +21,7 @@ }; | ||
/** get union of object method return types */ | ||
declare type ReturnTypeUnion<T extends { | ||
[key: string]: () => any; | ||
}> = ObjectValueTypes<FlattenToReturnTypes<T>>; | ||
declare type ReturnTypeUnion<T extends MethodObject> = ObjectValueTypes<FlattenToReturnTypes<T>>; | ||
/** | ||
* Get union of actions types from a ImmerReducer class | ||
*/ | ||
export declare type Actions<T extends ImmerReducerClass> = ReturnTypeUnion<ActionCreators<T>>; | ||
/** type constraint for the ImmerReducer class */ | ||
@@ -40,3 +43,3 @@ export interface ImmerReducerClass { | ||
/** ActionCreator function interface with actual action type name */ | ||
interface ActionCreator<ActionTypeType, Payload extends any[]> { | ||
interface ImmerActionCreator<ActionTypeType, Payload extends any[]> { | ||
readonly type: ActionTypeType; | ||
@@ -50,4 +53,13 @@ (...args: Payload): { | ||
export declare type ActionCreators<ClassActions extends ImmerReducerClass> = { | ||
[K in keyof Methods<InstanceType<ClassActions>>]: ActionCreator<K, ArgumentsType<InstanceType<ClassActions>[K]>>; | ||
[K in keyof Methods<InstanceType<ClassActions>>]: ImmerActionCreator<K, ArgumentsType<InstanceType<ClassActions>[K]>>; | ||
}; | ||
/** | ||
* Type guard for detecting actions created by immer reducer | ||
* | ||
* @param action any redux action | ||
* @param immerActionCreator method from a ImmerReducer class | ||
*/ | ||
export declare function isAction<A extends ImmerActionCreator<any, any>>(action: { | ||
type: any; | ||
}, immerActionCreator: A): action is ReturnType<A>; | ||
/** The actual ImmerReducer class */ | ||
@@ -54,0 +66,0 @@ export declare class ImmerReducer<T> { |
@@ -8,2 +8,12 @@ "use strict"; | ||
var actionTypePrefix = "IMMER_REDUCER"; | ||
/** | ||
* Type guard for detecting actions created by immer reducer | ||
* | ||
* @param action any redux action | ||
* @param immerActionCreator method from a ImmerReducer class | ||
*/ | ||
function isAction(action, immerActionCreator) { | ||
return action.type === immerActionCreator.type; | ||
} | ||
exports.isAction = isAction; | ||
/** The actual ImmerReducer class */ | ||
@@ -10,0 +20,0 @@ var ImmerReducer = /** @class */ (function () { |
{ | ||
"name": "immer-reducer", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "", | ||
@@ -17,2 +17,6 @@ "main": "lib/immer-reducer.js", | ||
}, | ||
"keywords": [ | ||
"typescript", | ||
"immer" | ||
], | ||
"author": "", | ||
@@ -19,0 +23,0 @@ "license": "ISC", |
@@ -198,7 +198,61 @@ # immer-reducer | ||
## API | ||
## Helpers | ||
- `createReducerFunction(klass: ImmerReducer: initialState?: Object)` | ||
- `createActionCreators(klass: ImmerReducer)` | ||
- `setPrefix(prefix: string)` | ||
- The default prefix in actions is is `IMMER_REDUCER`. Call this customize it for you app. | ||
The module exports following helpers | ||
### `function isAction(action, actionCreator)` | ||
<!-- | ||
Type | ||
```ts | ||
function isAction( | ||
action: {type: any}, | ||
ActionCreator: ImmerActionCreator, | ||
): boolean; | ||
``` --> | ||
Type guard for detecting actions generated by immer-reducer. This is useful | ||
for example when you need finer type-safe action control with redux-saga's | ||
function based patterns with `take(pattern)`. | ||
Example | ||
```ts | ||
if (isAction(someAction, ActionCreators.setFirstName)) { | ||
someAction.type; // Type checks to `"setFirstName"` | ||
someAction.payload; // Type checks `[string]` | ||
} | ||
``` | ||
### `type Actions<ImmerReducerClass>` | ||
Get union of the action types generated by the ImmerReducer class | ||
Example | ||
```ts | ||
type MyActions = Actions<typeof MyImmerReducer>; | ||
// Same as | ||
type MyActions = | ||
| { | ||
type: "setFirstName"; | ||
payload: [string]; | ||
} | ||
| { | ||
type: "setLastName"; | ||
payload: [string]; | ||
}; | ||
``` | ||
### `function setPrefix(prefix: string)` | ||
The default prefix in the generated action types is `IMMER_REDUCER`. Call | ||
this customize it for your app. | ||
Example | ||
```ts | ||
setPrefix("MY_APP"); | ||
``` |
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
20037
214
258