Skip to content

Commit

Permalink
Merge pull request reduxjs#3536 from reduxjs/ts-conversion
Browse files Browse the repository at this point in the history
[Meta PR] Convert to TypeScript

Co-authored-by: Gregory Beaver <greg.beaver.cello@gmail.com>
Co-authored-by: Nick McCurdy <nick@nickmccurdy.com>
Co-authored-by: Jed Mao <jedmao@users.noreply.github.com>
Former-commit-id: 3b4f495
Former-commit-id: d429872
  • Loading branch information
4 people committed Sep 6, 2019
2 parents 80d7c23 + b2dd15b commit 8466262
Show file tree
Hide file tree
Showing 61 changed files with 135 additions and 78 deletions.
1 change: 1 addition & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { NODE_ENV } = process.env

module.exports = {
presets: [
'@babel/typescript',
[
'@babel/env',
{
Expand Down
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
**/server.js
**/webpack.config*.js
**/flow-typed/**
# TODO: figure out a way to lint this using flow instead of typescript
examples/todos-flow
34 changes: 23 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
module.exports = {
extends: 'react-app',

parser: '@typescript-eslint/parser',

plugins: ['@typescript-eslint'],

settings: {
react: {
version: '16.8'
},
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx']
},
'import/resolver': {
// use <root>/tsconfig.json
typescript: {}
}
},

rules: {
'jsx-a11y/href-no-hash': 'off'
},

overrides: [
{
files: 'test/**/*.js',
env: {
jest: true,
},
},
],
'jsx-a11y/href-no-hash': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
vars: 'all',
args: 'after-used',
ignoreRestSiblings: true,
argsIgnorePattern: '^_' // ignore unused variables whose name is '_'
}
]
}
}
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
.DS_Store
*.log
node_modules

coverage

dist
lib
es
coverage
types

website/translated_docs
website/build/
Expand Down
11 changes: 5 additions & 6 deletions examples/todomvc/src/components/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import App from './App'
import Header from '../containers/Header'
import MainSection from '../containers/MainSection'


const setup = propOverrides => {
const setup = _propOverrides => {
const renderer = createRenderer()
renderer.render(<App />)
const output = renderer.getRenderOutput()
Expand All @@ -16,16 +15,16 @@ describe('components', () => {
describe('Header', () => {
it('should render', () => {
const output = setup()
const [ header ] = output.props.children
const [header] = output.props.children
expect(header.type).toBe(Header)
})
})

describe('Mainsection', () => {
it('should render', () => {
const output = setup()
const [ , mainSection ] = output.props.children
const [, mainSection] = output.props.children
expect(mainSection.type).toBe(MainSection)
})
})
})
})
2 changes: 1 addition & 1 deletion index.d.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50e8d018faee8887ed82337fcd1993ad07df0968
aa8ec2ad318f2542aaf407c9112143f8669dba4e
2 changes: 1 addition & 1 deletion package-lock.json.REMOVED.git-id

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6dc69e8d7768ccf0b08dd7b6ed73f57ce5ba7510
772f99d6b51677bd2cd88345904b9e29c40eb9bd
2 changes: 1 addition & 1 deletion rollup.config.js.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
07297f2bc2205b6f21539ca6f1c50d96d4cb9096
27ce0b7bc959177b6a0d4a3dd4ae43fe27f476d3
1 change: 0 additions & 1 deletion src/applyMiddleware.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions src/applyMiddleware.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f73584325282b69e12dd6caa61ae5bf23c32e64f
1 change: 0 additions & 1 deletion src/bindActionCreators.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions src/bindActionCreators.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
073a3b58df26f159289b9e854cfd615af6ed9bc9
1 change: 0 additions & 1 deletion src/combineReducers.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions src/combineReducers.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
07f9f7bd22ae1cc4dd9a295731960e1e90aac88e
22 changes: 0 additions & 22 deletions src/compose.js

This file was deleted.

1 change: 1 addition & 0 deletions src/compose.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fcbfd70d7bf2aa262de58be558c4a6008957e7f0
1 change: 0 additions & 1 deletion src/createStore.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions src/createStore.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a75fc2a304526089ddb4af859689430dc4e4b3c6
1 change: 0 additions & 1 deletion src/index.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions src/index.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
40df5f346de6cd075ce8946e0fa1aa778e9cbc09
1 change: 1 addition & 0 deletions src/types/actions.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
75215ed7afdeaf3f2e973109805cc779557c8bfb
1 change: 1 addition & 0 deletions src/types/middleware.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d631d988daabb5d9d72c5821122fede3a8e705b2
1 change: 1 addition & 0 deletions src/types/reducers.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d0c41a9d33ce27ca26c1a283564be5b04a6d55e3
1 change: 1 addition & 0 deletions src/types/store.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaebb0ad21e3f200568600eb568abbec514e7111
File renamed without changes.
6 changes: 3 additions & 3 deletions src/utils/isPlainObject.js → src/utils/isPlainObject.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @param {any} obj The object to inspect.
* @returns {boolean} True if the argument appears to be a plain object.
* @param obj The object to inspect.
* @returns True if the argument appears to be a plain object.
*/
export default function isPlainObject(obj) {
export default function isPlainObject(obj: any): boolean {
if (typeof obj !== 'object' || obj === null) return false

let proto = obj
Expand Down
5 changes: 2 additions & 3 deletions src/utils/warning.js → src/utils/warning.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* Prints a warning in the console if it exists.
*
* @param {String} message The warning message.
* @returns {void}
* @param message The warning message.
*/
export default function warning(message) {
export default function warning(message: string): void {
/* eslint-disable no-console */
if (typeof console !== 'undefined' && typeof console.error === 'function') {
console.error(message)
Expand Down
1 change: 0 additions & 1 deletion test/applyMiddleware.spec.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions test/applyMiddleware.spec.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fefd849b15428a4a294651edc81a3cb7fc373893
1 change: 0 additions & 1 deletion test/bindActionCreators.spec.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions test/bindActionCreators.spec.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5efecb6902f7abeab5af8892f778346c4bcaeaf7
1 change: 0 additions & 1 deletion test/combineReducers.spec.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions test/combineReducers.spec.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
161e00658b64c2fa549317d5b6f68bc83f8646fe
1 change: 0 additions & 1 deletion test/compose.spec.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions test/compose.spec.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d25131cdebad8959ef7eba061752516823b623a8
1 change: 0 additions & 1 deletion test/createStore.spec.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions test/createStore.spec.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5e8ef1db50fc59beceb2aee464b31644368a3669
1 change: 0 additions & 1 deletion test/helpers/actionCreators.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions test/helpers/actionCreators.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
46964ea9cef1aec64487cde982fc7450877410d7
File renamed without changes.
4 changes: 0 additions & 4 deletions test/helpers/middleware.js

This file was deleted.

12 changes: 12 additions & 0 deletions test/helpers/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MiddlewareAPI, Dispatch, AnyAction } from '../..'

type ThunkAction<T extends any = any> = T extends AnyAction
? AnyAction
: T extends Function
? T
: never

export function thunk({ dispatch, getState }: MiddlewareAPI) {
return (next: Dispatch) => <T>(action: ThunkAction) =>
typeof action === 'function' ? action(dispatch, getState) : next(action)
}
1 change: 0 additions & 1 deletion test/helpers/reducers.js.REMOVED.git-id

This file was deleted.

1 change: 1 addition & 0 deletions test/helpers/reducers.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c10d3d017ed4b5122d25d5ed2b80309ccadf7cf9
18 changes: 18 additions & 0 deletions test/replaceReducers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createStore, combineReducers } from '..'

describe('replaceReducers test', () => {
it('returns the original store', () => {
const nextReducer = combineReducers({
foo: (state = 1, _action) => state,
bar: (state = 2, _action) => state
})
const store = createStore((state, action) => {
if (state === undefined) return { type: 5 }
return action
})

const nextStore = store.replaceReducer(nextReducer)

expect(nextStore).toBe(store)
})
})
File renamed without changes.
7 changes: 7 additions & 0 deletions test/typescript/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
rules: {
'no-unused-expressions': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-unused-vars': 'off'
}
}
2 changes: 1 addition & 1 deletion test/typescript/actionCreators.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e3594a0712d844fe26a2d8836137624dd9e1e73c
f7a15bcd754e609d63e23bddd636288c72d72c1b
2 changes: 1 addition & 1 deletion test/typescript/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action as ReduxAction } from 'redux'
import { Action as ReduxAction } from '../..'

namespace FSA {
interface Action<P> extends ReduxAction {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/compose.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ae2fc5b32dba9f6959659bfcfc901d366ce0217a
cfc608bad3e6370697f8f4ae049df1efc823cd8f
2 changes: 1 addition & 1 deletion test/typescript/dispatch.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72f89988562262cb211a90298d9ca75c3b37bfa4
9cb125e37a001777f5a3a86146a6b06a746cbbf9
2 changes: 1 addition & 1 deletion test/typescript/enhancers.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7259b4884bf70423a7abbd95ced2ef45a9a760c1
c6d29af6a08539a343f95fedf50be0f8a2927bfa
2 changes: 1 addition & 1 deletion test/typescript/injectedDispatch.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f115b699fe3596e938591ff39767c65032362da0
46dc9a70160a8144aae157d53f4180200b6e4f13
2 changes: 1 addition & 1 deletion test/typescript/middleware.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7b1c48443c1f0e03751a219d2a4f97258efabfd5
a0f9afa7f68a7af687107984478c7c4b74367dbd
2 changes: 1 addition & 1 deletion test/typescript/reducers.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5a109eeecee5610220b77259d3b0ee175d8dbd74
f013f83b37aaf1bf8d8183e2bc433a454fa56e9b
26 changes: 26 additions & 0 deletions test/typescript/replaceReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { combineReducers, createStore } from '../..'

/**
* verify that replaceReducer maintains strict typing if the new types change
*/
const bar = (state = { value: 'bar' }) => state
const baz = (state = { value: 'baz' }) => state
const ACTION = {
type: 'action'
}

const originalCompositeReducer = combineReducers({ bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)

const firstState = store.getState()
firstState.bar.value
// typings:expect-error
firstState.baz.value

const nextStore = store.replaceReducer(combineReducers({ baz })) // returns -> { baz: { value: 'baz' }}

const nextState = nextStore.getState()
// typings:expect-error
nextState.bar.value
nextState.baz.value
2 changes: 1 addition & 1 deletion test/typescript/store.ts.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123464ce556358be71cad6cb468910c36c2f9a39
dd7d76296eeac8dd7d6abc7d65a98e7f24bb5bba
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('isPlainObject', () => {
expect(isPlainObject(new Date())).toBe(false)
expect(isPlainObject([1, 2, 3])).toBe(false)
expect(isPlainObject(null)).toBe(false)
expect(isPlainObject()).toBe(false)
expect(isPlainObject(undefined)).toBe(false)
expect(isPlainObject({ x: 1, y: 2 })).toBe(true)
})
})
File renamed without changes.
1 change: 1 addition & 0 deletions tsconfig.json.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1ef2e9f083e0338a4cf39a2cd6a11df70b0c3abc

0 comments on commit 8466262

Please sign in to comment.