Skip to content

Commit 0e139e9

Browse files
deniswmarkerikson
authored andcommitted
TypeScript rewrite (#73)
* Update to Babel 7 This is a pre-requisite to using @babel/preset-typescript later. This change required upgrading rollup-plugin-babel, which in turn required upgrading Rollup.js itself. Fortunately, the upgraded rollup-plugin-babel automatically takes care of Babel helpers deduplication and turning off module transpilation in Babel's env preset, so the Babel configuration could be drastically simplified. For the same reason, we don't need rollup-plugin-replace anymore. * Remove unused @babel/plugin-external-helpers * Add Babel Typescript preset and port isPlainObject * Port createAction() to TypeScript * Port configureStore() to TypeScript * Fix Rollup configuration * Fix type errors in serializableStateInvariantMiddleware.test.ts * Port createReducer() to TypeScript * Port createSlice() to TypeScript * Port index.js to TS and add index.d.ts * Add some typings-tester tests * Add "target" to tsconfig.json It's apparently not strictly needed for Babel's TypeScript plugin, but at least more explicit. * Use `T extends string` instaead of `string` as action type This allows more concrete types like `Action<'increment'>` to be inferred by TypeScript. * Remove unneeded extension from Rollup input path * Remove unneeded type parameter * Avoid a type cast in createReducer() * Type `isPlainObject` argument as `unknown` * Make findNonSerializableValue() typing more precise * Remove redundant "lib" in tsconfig.json It's implied by "target": "es2018". * Enable "esModuleInterop" in tsconfig.json * Update typings-tester tsconfig.json * Fix configureStore() state type inference edge case When passing an object as `reducer` and at the same time `preloadedState`, TypeScript infers the store state type from the latter although `preloadedState` is potentially just a partial state. An additional `PS` type parameter fixes this. * Fix configureStore() state inference without extra type Thanks to @Dudeonyx for this hack. * Introduce EnhancedStore type for thunk dispatch * Update formatting commands * Reformat, again * Package lock random changes * Conditionally import immutable-state-invariant middleware * Make createReducer() return Reducer<Immutable<S>, A> This reflects the actual runtime behavior of immer (which freezes produced values) and fixes a type error with immer >= 1.10.3. * Revert createReducer() type change Things are fixed in v1.10.5. * 0.4.0-0 * Run TypeScript compiler in `prepare` * Configure ESLint for TypeScript For now, we need to use the Git version of typescript-eslint-parser because the last release only supports TypeScript 3.1. A new release is planned for any day now, so we should be able to move to an NPM release soon. eslint/typescript-eslint-parser#596 As ESLint's no-unused-vars doesn't work well with TypeScript, I enabled noUnusedLocals and noUnusedParameters on the TypeScript side instead. * Generate TypeScript declaration files in dist/ * Revert "Generate TypeScript declaration files in dist/" This reverts commit e449c8a. * Publish src/ for TypeScript typings * 0.4.0-1
1 parent 2bb3a59 commit 0e139e9

38 files changed

+2625
-1260
lines changed

.babelrc

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
11
{
2-
"presets": [
3-
[
4-
"env",
5-
{
6-
"modules": false
7-
}
8-
]
9-
],
10-
"env": {
11-
"development": {
12-
"plugins": ["external-helpers"]
13-
},
14-
"test": {
15-
"plugins": ["transform-es2015-modules-commonjs"]
16-
}
17-
}
2+
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
183
}

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
module.exports = {
22
extends: 'react-app',
3+
parser: 'typescript-eslint-parser',
34

45
rules: {
5-
'jsx-a11y/href-no-hash': 'off'
6+
'jsx-a11y/href-no-hash': 'off',
7+
// Taken care of by TypeScript's `noUnusedLocals` / `noUnusedParameters`
8+
'no-unused-vars': 'off'
69
}
710
}

README.md

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1-
# Redux Starter Kit
2-
3-
[![build status](https://img.shields.io/travis/reduxjs/redux-starter-kit/master.svg?style=flat-square)](https://travis-ci.org/reduxjs/redux-starter-kit)
4-
[![npm version](https://img.shields.io/npm/v/redux-starter-kit.svg?style=flat-square)](https://www.npmjs.com/package/redux-starter-kit)
5-
[![npm downloads](https://img.shields.io/npm/dm/redux-starter-kit.svg?style=flat-square)](https://www.npmjs.com/package/redux-starter-kit)
6-
7-
**A simple set of tools to make using Redux easier**
8-
9-
`npm install redux-starter-kit`
10-
11-
(Special thanks to Github user @shotak for donating to the package name.)
12-
13-
### Purpose
14-
15-
The `redux-starter-kit` package is intended to help address three common complaints about Redux:
16-
17-
- "Configuring a Redux store is too complicated"
18-
- "I have to add a lot of packages to get Redux to do anything useful"
19-
- "Redux requires too much boilerplate code"
20-
21-
We can't solve every use case, but in the spirit of [`create-react-app`](https://github.com/facebook/create-react-app) and [`apollo-boost`](https://dev-blog.apollodata.com/zero-config-graphql-state-management-27b1f1b3c2c3), we can try to provide some tools that abstract over the setup process and handle the most common use cases, as well as include some useful utilities that will let the user simplify their application code.
22-
23-
This package is _not_ intended to solve every possible complaint about Redux, and is deliberately limited in scope. It does _not_ address concepts like "reusable encapsulated Redux modules", data fetching, folder or file structures, managing entity relationships in the store, and so on.
24-
25-
### What's Included
26-
27-
`redux-starter-kit` includes:
28-
29-
- A `configureStore()` function with simplified configuration options. It can automatically combine your slice reducers, adds whatever Redux middleware you supply, includes `redux-thunk` by default, and enables use of the Redux DevTools Extension.
30-
- A `createReducer()` utility that lets you supply a lookup table of action types to case reducer functions, rather than writing switch statements. In addition, it automatically uses the [`immer` library](https://github.com/mweststrate/immer) to let you write simpler immutable updates with normal mutative code, like `state.todos[3].completed = true`.
31-
- A `createAction()` utility that returns an action creator function for the given action type string. The function itself has `toString()` defined, so that it can be used in place of the type constant.
32-
- A `createSlice()` function that accepts a set of reducer functions, a slice name, and an initial state value, and automatically generates corresponding action creators, types, and simple selector functions.
33-
- An improved version of the widely used `createSelector` utility for creating memoized selector functions, which can accept string keypaths as "input selectors" (re-exported from the [`selectorator` library](https://github.com/planttheidea/selectorator)).
34-
35-
## Documentation
36-
37-
The `redux-starter-kit` docs are now published at **https://redux-starter-kit.js.org**.
38-
39-
We're currently expanding and rewriting our docs content - check back soon for more updates!
1+
# Redux Starter Kit
2+
3+
[![build status](https://img.shields.io/travis/reduxjs/redux-starter-kit/master.svg?style=flat-square)](https://travis-ci.org/reduxjs/redux-starter-kit)
4+
5+
[![npm version](https://img.shields.io/npm/v/redux-starter-kit.svg?style=flat-square)](https://www.npmjs.com/package/redux-starter-kit)
6+
7+
**A simple set of tools to make using Redux easier**
8+
9+
`npm install redux-starter-kit`
10+
11+
(Special thanks to Github user @shotak for dona
12+
13+
**A simple set of tools to make using Redux easier**
14+
15+
`npm install redux-starter-kit`
16+
17+
(Special thanks to Github user @shotak for donating to the package name.)
18+
19+
### Purpose
20+
21+
The `redux-starter-kit` package is intended to help address three common complaints about Redux:
22+
23+
- "Configuring a Redux store is too complicated"
24+
- "I have to add a lot of packages to get Redux to do anything useful"
25+
- "Redux requires too much boilerplate code"
26+
27+
We can't solve every use case, but in the spirit of le complaint about Redux, and is deliberately limited in scope. It and _not_ address concepts like "reusable encapsulated Redux modules", data fetching, folder or file s, we can try to provide some tools that abstract over the setup process and handle the most common use cases, as well as include some useful utilities that will let the user simplify their application code.
28+
29+
This package is _not_ intended to solve every possible complaint about Redux, and is deliberately limited in scope. It does _not_ address concepts like "reusable encapsulated Redux modules", data fetching, folder or file structures, managing entity relationships in the store, and so on.
30+
31+
### What's Included
32+
33+
`redux-starter-kit` includes:
34+
35+
- A `configureStore()` function with simplified configuration options. It can automatically combine your slice reducers, adds whatever Redux middleware you supply, includes `redux-thunk` by default, and enables use of the Redux DevTools Extension.
36+
- A `createReducer()` utility that lets you supply a lookup table of action types to case reducer functions, rather than writing switch statements. In addition, it automatically uses the e selector functions.
37+
- An improved version of the wid to let you write simpler immutable updates with normal mutative code, like `state.todos[3].completed = true`.
38+
- A `createAction()` utility that returns an action creator function for the given action type string. The function itself has `toString()` defined, so that it can be used in place of the type constant.
39+
- A `createSlice()` function that accepts a set of reducer functions, a slice name, and an initial state value, and automatically generates corresponding action creators, types, and simple selector functions.
40+
- An improved version of the widely used `createSelector` utility for creating memoized selector functions, which can accept string keypaths as "input selectors" (re-exported from the n for more updates!).
41+
42+
## Documentation
43+
44+
The `redux-starter-kit` docs are now published at **tent - check back soon for more **.
45+
46+
We're currently expanding and rewriting our docs content - check back soon for more updates!

docs/api/createReducer.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ hide_title: true
77

88
# `createReducer()`
99

10-
A utility that simplifies creating Redux reducer functions, by defining them as lookup tables of functions to handle each action type. It also allows you to drastically simplify immutable update logic, by writing "mutative" code inside your reducers.
10+
A utility that simplifies creating Redux reducer functions, by defining them as lookup tables of functions to handle each action type. It also allows you to drastically simplify immutable update logic, by writing "mutative" code inside your reducers.
1111

1212
Redux [reducers](https://redux.js.org/basics/reducers) are often implemented using a `switch` statement, with one `case` for every handled action type.
1313

@@ -49,7 +49,7 @@ const counterReducer = createReducer(0, {
4949

5050
## Direct State Mutation
5151

52-
Redux requires reducer functions to be pure and treat state values as immutable. While this is essential for making state updates predictable and observable, it can sometimes make the implementation of such updates awkward. Consider the following example:
52+
Redux requires reducer functions to be pure and treat state values as immutable. While this is essential for making state updates predictable and observable, it can sometimes make the implementation of such updates awkward. Consider the following example:
5353

5454
```js
5555
const addTodo = createAction('todos/add')
@@ -72,7 +72,7 @@ const todosReducer = createReducer([], {
7272
})
7373
```
7474

75-
The `addTodo` reducer is pretty easy to follow if you know the [ES6 spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax). However, the code for `toggleTodo` is much less straightforward, especially considering that it only sets a single flag.
75+
The `addTodo` reducer is pretty easy to follow if you know the [ES6 spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax). However, the code for `toggleTodo` is much less straightforward, especially considering that it only sets a single flag.
7676

7777
To make things easier, `createReducer` uses [immer](https://github.com/mweststrate/immer) to let you write reducers as if they were mutating the state directly. In reality, the reducer receives a proxy state that translates all mutations into equivalent copy operations.
7878

@@ -97,7 +97,7 @@ const todosReducer = createReducer([], {
9797
})
9898
```
9999

100-
If you choose to write reducers in this style, make sure to learn about the [pitfalls mentioned in the immer docs](https://github.com/mweststrate/immer#pitfalls) . Most importantly, you need to ensure that you either mutate the `state` argument or return a new state, _but not both_. For example, the following reducer would throw an exception if a `toggleTodo` action is passed:
100+
If you choose to write reducers in this style, make sure to learn about the [pitfalls mentioned in the immer docs](https://github.com/mweststrate/immer#pitfalls) . Most importantly, you need to ensure that you either mutate the `state` argument or return a new state, _but not both_. For example, the following reducer would throw an exception if a `toggleTodo` action is passed:
101101

102102
```js
103103
const todosReducer = createReducer([], {
@@ -111,11 +111,7 @@ const todosReducer = createReducer([], {
111111
// ... and returns a new value. This will throw an
112112
// exception. In this example, the easiest fix is
113113
// to remove the `return` statement.
114-
return [
115-
...state.slice(0, index),
116-
todo,
117-
...state.slice(index + 1)
118-
]
114+
return [...state.slice(0, index), todo, ...state.slice(index + 1)]
119115
}
120116
})
121117
```

index.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export { default as createNextState } from 'immer'
2+
export {
3+
Action,
4+
ActionCreator,
5+
AnyAction,
6+
Middleware,
7+
Reducer,
8+
Store,
9+
StoreEnhancer
10+
} from 'redux'
11+
export { default as createSelector } from 'selectorator'
12+
13+
export {
14+
configureStore,
15+
ConfigureStoreOptions,
16+
getDefaultMiddleware
17+
} from './src/configureStore'
18+
export {
19+
createAction,
20+
getType,
21+
PayloadAction,
22+
PayloadActionCreator
23+
} from './src/createAction'
24+
export { createReducer } from './src/createReducer'
25+
export { createSlice, CreateSliceOptions, Slice } from './src/createSlice'
26+
export {
27+
default as createSerializableStateInvariantMiddleware,
28+
isPlain
29+
} from './src/serializableStateInvariantMiddleware'

0 commit comments

Comments
 (0)