Skip to content

Commit 75b679d

Browse files
author
Kylie Stewart
authored
Update types to use generics & add a TS test for react-redux (#80)
* Add a test for react-redux's useSelector hook * Separate react-redux test from main usage test * Fix types, use generics
1 parent e01e975 commit 75b679d

File tree

5 files changed

+795
-642
lines changed

5 files changed

+795
-642
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- [#80](https://github.com/FormidableLabs/react-fast-compare/pull/80). Update types to use generic `any`s.
6+
- [#77](https://github.com/FormidableLabs/react-fast-compare/pull/77). Add tests for our TypeScript type definitions.
7+
38
## 3.1.0 (2020-05-08)
49

510
- [#76](https://github.com/FormidableLabs/react-fast-compare/pull/76). Add support for preact/compat.
@@ -17,6 +22,7 @@
1722
## 3.0.0 (2020-01-05)
1823

1924
**Features:**
25+
2026
- [#36](https://github.com/FormidableLabs/react-fast-compare/pull/36). Update to `[email protected]` with modified support for ES.next data types: `Map`, `Set`, `ArrayBuffer`.
2127
- [#57](https://github.com/FormidableLabs/react-fast-compare/pull/57). Minor refactoring to reduce min+gz size.
2228
- [#59](https://github.com/FormidableLabs/react-fast-compare/pull/59). Rename exported to `isEqual` for TypeScript users.

index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
declare function isEqual(a: any, b: any): boolean;
2-
declare namespace isEqual {}
1+
declare function isEqual<A = any, B = any>(a: A, b: B): boolean;
32
export = isEqual;

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
"preversion": "yarn test",
99
"benchmark": "node benchmark",
1010
"eslint": "eslint \"*.js\" benchmark test",
11-
"tslint": "eslint test/typescript/sample-usage.tsx",
11+
"tslint": "eslint test/typescript/*.tsx",
1212
"test-browser": "karma start test/browser/karma.conf.js",
1313
"test-browser-ie": "karma start test/browser/karma.conf.ie.js",
1414
"test-node": "mocha \"test/node/*.spec.js\"",
1515
"test-node-cov": "nyc mocha \"test/node/*.spec.js\"",
16-
"test-ts-usage": "tsc --esModuleInterop --jsx react --noEmit test/typescript/sample-usage.tsx",
16+
"test-ts-usage": "tsc --esModuleInterop --jsx react --noEmit test/typescript/sample-react-redux-usage.tsx test/typescript/sample-usage.tsx",
1717
"test-ts-defs": "tsc --target ES5 index.d.ts",
1818
"test": "builder concurrent --buffer eslint tslint test-ts-usage test-ts-defs test-node-cov test-browser",
1919
"test-ie": "builder concurrent --buffer eslint tslint test-ts-usage test-ts-defs test-node-cov test-browser-ie",
@@ -45,6 +45,8 @@
4545
"@testing-library/preact": "^1.0.2",
4646
"@types/node": "^14.0.1",
4747
"@types/react": "^16.9.35",
48+
"@types/react-dom": "^16.9.8",
49+
"@types/react-redux": "^7.1.9",
4850
"@typescript-eslint/parser": "^2.34.0",
4951
"babel-loader": "^8.0.6",
5052
"benchmark": "^2.1.4",
@@ -71,7 +73,10 @@
7173
"nyc": "^14.1.1",
7274
"preact": "^10.4.1",
7375
"react": "^16.3.1",
76+
"react-dom": "^16.13.1",
77+
"react-redux": "^7.2.0",
7478
"react-test-renderer": "^16.13.1",
79+
"redux": "^4.0.5",
7580
"shallow-equal-fuzzy": "0.0.2",
7681
"sinon": "^7.5.0",
7782
"terser": "^4.4.3",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This file exists to test our types against sample user code
2+
// This is compiled using `tsc` in our `test-ts-usage` script
3+
import React from 'react';
4+
import ReactDOM from 'react-dom';
5+
import { Provider, useSelector } from 'react-redux';
6+
import { createStore } from 'redux';
7+
8+
import equal from '../../index.js';
9+
10+
type IState = {
11+
items: string[];
12+
};
13+
14+
type IAction = {
15+
type: string;
16+
payload: any;
17+
};
18+
19+
const initialState: IState = {
20+
items: [],
21+
};
22+
23+
const reducer = (state: IState, action: IAction) => {
24+
return state;
25+
};
26+
27+
const lengthSelector = (state: IState): number => state.items.length;
28+
29+
const store = createStore(reducer, initialState);
30+
31+
function Test() {
32+
const length = useSelector(lengthSelector, equal);
33+
return (
34+
<div>
35+
Testing react-redux useSelector. There are
36+
{length.toExponential()} items.
37+
</div>
38+
);
39+
}
40+
41+
ReactDOM.render(
42+
<Provider store={store}>
43+
<Test />
44+
</Provider>,
45+
document.getElementById('root')
46+
);

0 commit comments

Comments
 (0)