react-redux-typescript
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -1,8 +0,2 @@ | ||
import { EmptyAction, PayloadAction } from './types'; | ||
/** | ||
* @export Action Creator helper factory function | ||
* @class ActionCreator | ||
* @template T - Generic Type | ||
* @template P - Generic Type | ||
*/ | ||
import { EmptyAction, PayloadAction } from './redux-types'; | ||
export declare class ActionCreator<T, P> { | ||
@@ -17,16 +11,3 @@ readonly type: T; | ||
} | ||
/** | ||
* @export createEmptyAction - empty action creator function | ||
* @template T - Generic Type | ||
* @param type: T | ||
* @returns () => EmptyAction<T> | ||
*/ | ||
export declare function createEmptyAction<T>(type: T): () => EmptyAction<T>; | ||
/** | ||
* @export createPayloadAction - FSA action creator function | ||
* @template T - Generic Type | ||
* @template P - Generic Type | ||
* @param type: T | ||
* @returns (payload: P) => PayloadAction<T, P> | ||
*/ | ||
export declare function createPayloadAction<T, P>(type: T): (payload: P) => PayloadAction<T, P>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @export Action Creator helper factory function | ||
* @class ActionCreator | ||
* @template T - Generic Type | ||
* @template P - Generic Type | ||
*/ | ||
class ActionCreator { | ||
@@ -16,8 +10,2 @@ constructor(type) { | ||
exports.ActionCreator = ActionCreator; | ||
/** | ||
* @export createEmptyAction - empty action creator function | ||
* @template T - Generic Type | ||
* @param type: T | ||
* @returns () => EmptyAction<T> | ||
*/ | ||
function createEmptyAction(type) { | ||
@@ -27,10 +15,2 @@ return () => ({ type }); | ||
exports.createEmptyAction = createEmptyAction; | ||
; | ||
/** | ||
* @export createPayloadAction - FSA action creator function | ||
* @template T - Generic Type | ||
* @template P - Generic Type | ||
* @param type: T | ||
* @returns (payload: P) => PayloadAction<T, P> | ||
*/ | ||
function createPayloadAction(type) { | ||
@@ -37,0 +17,0 @@ return (payload) => ({ type, payload }); |
@@ -1,8 +0,4 @@ | ||
/** | ||
* @author Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io) | ||
* @copyright Copyright (c) 2016 Piotr Witek | ||
* @license MIT | ||
*/ | ||
export * from './types'; | ||
export * from './mapped-types'; | ||
export * from './redux-types'; | ||
export * from './helpers'; | ||
export * from './returntypeof'; |
@@ -1,14 +0,6 @@ | ||
/** | ||
* @author Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io) | ||
* @copyright Copyright (c) 2016 Piotr Witek | ||
* @license MIT | ||
*/ | ||
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./helpers")); | ||
// workarounds for missing features | ||
__export(require("./returntypeof")); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./helpers"), exports); | ||
tslib_1.__exportStar(require("./returntypeof"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,7 +0,2 @@ | ||
/** | ||
* @export returntypeof() - extract return type of an "expression" | ||
* @template RT - Generic Type | ||
* @param expression: (...params: any[]) => RT | ||
* @returns RT | ||
*/ | ||
export declare function returntypeof<RT>(expression: (...params: any[]) => RT): RT; | ||
export declare function getReturnOfExpression<RT>(expression: (...params: any[]) => RT): RT; | ||
export declare const returntypeof: typeof getReturnOfExpression; |
@@ -1,14 +0,8 @@ | ||
// Copyright (c) 2016 Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io) | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @export returntypeof() - extract return type of an "expression" | ||
* @template RT - Generic Type | ||
* @param expression: (...params: any[]) => RT | ||
* @returns RT | ||
*/ | ||
function returntypeof(expression) { | ||
return {}; | ||
function getReturnOfExpression(expression) { | ||
return null; | ||
} | ||
exports.returntypeof = returntypeof; | ||
exports.getReturnOfExpression = getReturnOfExpression; | ||
exports.returntypeof = getReturnOfExpression; | ||
//# sourceMappingURL=returntypeof.js.map |
{ | ||
"name": "react-redux-typescript", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "React / Redux / TypeScript Utils", | ||
@@ -9,5 +9,5 @@ "author": "Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)", | ||
"license": "MIT", | ||
"types": "es5/index.d.ts", | ||
"main": "es5/index.js", | ||
"module": "module/index.js", | ||
"types": "es5-commonjs/index.d.ts", | ||
"main": "es5-commonjs/index.js", | ||
"module": "es5-module/index.js", | ||
"jsnext:main": "jsnext/index.js", | ||
@@ -18,21 +18,23 @@ "engines": { | ||
"scripts": { | ||
"clean": "rm -rf node_modules/ es5/ module/ jsnext/", | ||
"reinstall": "rm -rf node_modules/ && yarn install", | ||
"prepublishOnly": "yarn run clean && yarn install && yarn run check && yarn run test && yarn run build", | ||
"clean": "rm -rf es5-commonjs/ es5-module/ jsnext/", | ||
"prepublishOnly": "yarn run clean && yarn run reinstall && yarn run check && yarn run test && yarn run build", | ||
"check": "yarn run lint & yarn run tsc", | ||
"lint": "tslint --project src/tsconfig.json", | ||
"tsc": "tsc -p src --noEmit", | ||
"test": "ts-node -P src src/specs/index.ts", | ||
"build": "yarn run build:es5 & yarn run build:module & yarn run build:jsnext", | ||
"build:es5": "rm -rf es5/ && tsc -p src --outDir es5/", | ||
"build:module": "rm -rf module/ && tsc -p src --outDir module/ -m 'ES2015'", | ||
"build:jsnext": "rm -rf jsnext/ && tsc -p src --outDir jsnext/ -t 'ES2015'" | ||
"lint": "tslint --project tsconfig.json", | ||
"tsc": "tsc -p . --noEmit", | ||
"test": "ts-node -P . tests.ts", | ||
"build": "yarn run build:commonjs & yarn run build:module & yarn run build:jsnext", | ||
"build:commonjs": "rm -rf es5-commonjs/ && tsc -p . --outDir es5-commonjs/", | ||
"build:module": "rm -rf es5-module/ && tsc -p . --outDir es5-module/ -m 'ES2015'", | ||
"build:jsnext": "rm -rf jsnext/ && tsc -p . --outDir jsnext/ -t 'ES2015'" | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"redux": "^3.7.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^7.0.0", | ||
"ts-node": "^2.0.0", | ||
"tslib": "^1.5.0", | ||
"tslint": "^4.3.1", | ||
"typescript": "^2.2.1" | ||
"@types/node": "6", | ||
"ts-node": "^3.3.0", | ||
"tslib": "1.8.0", | ||
"tslint": "5.7.0", | ||
"typescript": "2.5.3" | ||
}, | ||
@@ -39,0 +41,0 @@ "keywords": [ |
136
README.md
@@ -8,52 +8,23 @@ # React / Redux / TypeScript Utils | ||
### Table of Contents | ||
- [Helpers v2.2](#helpers-v22) | ||
- [Helpers v2.0](#helpers-v20) | ||
- [returntypeof polyfill](#returntypeof-polyfill) | ||
## Table of Contents (v3.0) | ||
--- | ||
### Redux Utils | ||
### Helpers v3.0 | ||
```ts | ||
WIP | ||
... | ||
``` | ||
### Mapped Types | ||
- [Diff](#diff) | ||
- [Omit](#omit) | ||
- [Overwrite](#overwrite) | ||
--- | ||
### Types Utils | ||
- [getReturnOfExpression](#getreturnofexpression) | ||
### Helpers v2.2 | ||
```ts | ||
> [Docs v2.X](/READMEv2.0.md) | ||
export class ActionCreator<T, P> { | ||
readonly type: T; | ||
readonly payload: P; | ||
--- | ||
constructor(type: T) { this.type = type; } | ||
create = (payload: P) => ({ type: this.type, payload }); | ||
} | ||
### Redux Utils | ||
// Example: | ||
import { ActionCreator } from 'react-redux-typescript'; | ||
```ts | ||
export const ActionCreators = { | ||
IncreaseCounter: new ActionCreator<'IncreaseCounter', number>('IncreaseCounter'), | ||
ChangeBaseCurrency: new ActionCreator<'ChangeBaseCurrency', string>('ChangeBaseCurrency'), | ||
}; | ||
store.dispatch(ActionCreators.IncreaseCounter.create(4)); // { type: "IncreaseCounter", payload: 4 } | ||
store.dispatch(ActionCreators.ChangeBaseCurrency.create('USD')); // { type: "ChangeBaseCurrency", payload: 'USD' } | ||
// Action Types | ||
type Action = typeof ActionCreators[keyof typeof ActionCreators]; | ||
// { type: "IncreaseCounter", payload: number } | { type: "ChangeBaseCurrency", payload: string } | ||
// Reducer vvvvvv | ||
export default function reducer(state: State = initialState, action: Action): State { | ||
if (action.type === ActionCreators.IncreaseCounter.type) { | ||
state.counter = action.payload; // number | ||
} | ||
else if (action.type === ActionCreators.ChangeBaseCurrency.type) { | ||
state.baseCurrency = action.payload; // string | ||
} | ||
... | ||
``` | ||
@@ -63,30 +34,20 @@ | ||
### Helpers v2.0 | ||
### Mapped Types | ||
#### Diff | ||
```ts | ||
/** | ||
* @type EmptyAction - Empty Action Type | ||
* @template T - Generic Type | ||
*/ | ||
export type EmptyAction<T> = { | ||
readonly type: T; | ||
} | ||
// TestDiff expects: ('a' | 'b') | ||
type TestDiff = Diff<'a' | 'b' | 'c', 'c' | 'd'>; | ||
``` | ||
/** | ||
* @type PayloadAction - Flux Standard Action Type | ||
* @template T - Generic Type | ||
* @template P - Generic Type | ||
*/ | ||
export type PayloadAction<T, P> = { | ||
readonly type: T; | ||
readonly payload: P; | ||
readonly error?: boolean; | ||
} | ||
#### Omit | ||
```ts | ||
// TestOmit expects: { b: number, c: boolean } | ||
type TestOmit = Omit<{ a: string, b: number, c: boolean }, 'a'>; | ||
``` | ||
export function createEmptyAction<T>(type: T): () => EmptyAction<T> { | ||
return () => ({ type }); | ||
}; | ||
export function createPayloadAction<T, P>(type: T): (payload: P) => PayloadAction<T, P> { | ||
return (payload) => ({ type, payload }); | ||
} | ||
#### Overwrite | ||
```ts | ||
// TestOverwrite expects: { b: number, c: boolean } & { a: number } | ||
type TestOverwrite = Overwrite<{ a: string, b: number, c: boolean }, { a: number }>; | ||
``` | ||
@@ -96,36 +57,27 @@ | ||
### returntypeof polyfill | ||
### Types Utils | ||
#### getReturnOfExpression | ||
> Get return value of an "expression" with inferred return type | ||
> alias: returntypeof | ||
https://github.com/Microsoft/TypeScript/issues/6606 | ||
```ts | ||
// returntypeof() - extract return type of an "expression" | ||
// this polyfill exist because TypeScript does not support this feature yet | ||
// this polyfill exist because TypeScript does not support getting type of expression | ||
// (tracking issue: https://github.com/Microsoft/TypeScript/issues/6606) | ||
export function returntypeof<RT>(expression: (...params: any[]) => RT): RT { | ||
return {} as RT; | ||
export function getReturnOfExpression<RT>( | ||
expression: (...params: any[]) => RT, | ||
): RT { | ||
return null as any as RT; | ||
} | ||
// Example: | ||
import { returntypeof } from 'react-redux-typescript'; | ||
import { getReturnOfExpression } from 'react-redux-typescript'; | ||
const mapStateToProps = (state: RootState) => ({ | ||
counter: state.counter, | ||
baseCurrency: state.baseCurrency, | ||
currencies: CurrencyRatesSelectors.getCurrencies(state), | ||
}); | ||
const increment = () => ({ type: 'INCREMENT' as 'INCREMENT' }); | ||
const dispatchToProps = { | ||
increaseCounter: ActionCreators.IncreaseCounter.create, | ||
changeBaseCurrency: ActionCreators.ChangeBaseCurrency.create, | ||
}; | ||
const returnOfIncrement = getReturnOfExpression(increment); | ||
type INCREMENT = typeof returnOfIncrement; // { type: "INCREMENT"; } | ||
``` | ||
// Props types inferred from mapStateToProps & dispatchToProps | ||
const stateProps = returntypeof(mapStateToProps); | ||
type Props = typeof stateProps & typeof dispatchToProps; | ||
type State = {}; | ||
class CurrencyConverterContainer extends React.Component<Props, State> { | ||
... | ||
} | ||
export default connect(mapStateToProps, dispatchToProps)(CurrencyConverterContainer); | ||
``` | ||
--- | ||
@@ -132,0 +84,0 @@ MIT License |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
52217
86
736
1
84
+ Addedredux@^3.7.2
+ Addedjs-tokens@4.0.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlodash-es@4.17.21(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedredux@3.7.2(transitive)
+ Addedsymbol-observable@1.2.0(transitive)