Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update types + add TS tests #77

Merged
merged 20 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@ $ yarn run test-browser-ie

### Types

We validate our TypeScript `index.d.ts` with:
We validate our TypeScript `index.d.ts` with two steps:

```sh
# Runs the TypeScript compiler over our types
$ yarn run test-tsc
kale-stew marked this conversation as resolved.
Show resolved Hide resolved

# Runs our types through a sample TypeScript file
$ yarn run test-ts
# Compiled output is put into a co-located index.js
# Treat the output like a snapshot, commit any changes
```

### Style
Expand Down Expand Up @@ -135,7 +141,13 @@ please flag to your reviewers and have a discussion about whether or not the siz

_Only for project administrators_

1. Run `npm version patch` (or `minor|major|VERSION`) to run tests and lint,
build published directories, then update `package.json` + add a git tag.
2. Run `npm publish` and publish to NPM if all is well.
3. Run `git push && git push --tags`
```sh
# (1) Run tests, lint, build published dir, update package.json
$ npm version [patch|minor|major|<version>]

# (2) If all is well, publish the new version to the npm registry
$ npm publish

# (3) Then, update github with the associated tag
$ git push && git push --tags
```
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare function isEqual(a: any, b: any): boolean;
declare function isEqual<A = any, B = any>(a: A, b: B): boolean;
declare namespace isEqual {}
export = isEqual;
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"test-browser": "karma start test/browser/karma.conf.js",
"test-browser-ie": "karma start test/browser/karma.conf.ie.js",
"test-node": "mocha \"test/node/*.spec.js\"",
"test-node-cov": "nyc mocha \"test/node/*.spec.js\"",
"test-ts": "tsc --target ES5 --noImplicitAny index.d.ts",
"test": "builder concurrent --buffer eslint test-ts test-node-cov test-browser",
"test-ie": "builder concurrent --buffer eslint test-ts test-node-cov test-browser-ie",
"test-node-cov": "nyc yarn test-node",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"test-ts": "tsc --p test/typescript/index.ts",
"test-tsc": "tsc --target ES5 --noImplicitAny index.d.ts",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"test": "builder concurrent --buffer eslint test-tsc test-ts test-node-cov test-browser",
"test-ie": "builder concurrent --buffer eslint test-tsc -test-ts test-node-cov test-browser-ie",
"compress": "terser --compress --mangle=\"toplevel:true\" -- index.js",
"size-min-gz": "yarn -s compress | gzip -9 | wc -c"
},
Expand Down
13 changes: 13 additions & 0 deletions test/typescript/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Test file for types in typescript!
// =============
// An example of `equal` usage that currently breaks:
// https://github.com/FormidableLabs/react-fast-compare/issues/61
// @types/react-redux/index.d.ts
// export function useSelector<TState, TSelected>(
// selector: (state: TState) => TSelected,
// equalityFn?: (left: TSelected, right: TSelected) => boolean
// ): TSelected;
// =============
// `a.default is not a function`
// // https://github.com/FormidableLabs/react-fast-compare/pulls/62
// Test usage of this module elsewhere? Or push a release and hope the upstream is fixed?
43 changes: 43 additions & 0 deletions test/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from "react";

const equal = require("../..");

type DeepObject = {
any: any;
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
};

type State = {
any: {
any: any;
};
};

// State
const selector = (state: State) => {
a: {
b: [1, 2, 3];
}
};

class TestComponent extends React.Component {
constructor() {
super();
this.useSelector = this.useSelector;
}

shouldComponentUpdate(nextProps) {
return !equal(this.props, nextProps);
}

useSelector<TState, TSelected>(
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
selector: (state: TState) => TSelected,
equalityFn?: (left: TSelected, right: TSelected) => boolean
): TSelected;

this.useSelector(selector); // typed correctly as DeepObject
this.useSelector(selector, equal); // typed as any

render() {
// ...
}
};