Skip to content

Typescript Typings #9

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

Merged
merged 17 commits into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- run:
name: Run Type checking
command: npm run flow -s
command: npm run types -s

- run:
name: Run Unit Tests
Expand Down
48 changes: 44 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:flowtype/recommended',
'plugin:prettier/recommended',
'prettier/react',
],
parserOptions: {
ecmaFeatures: {
Expand All @@ -18,20 +19,59 @@ module.exports = {
},
settings: {
react: {
version: 'latest',
version: 'detect',
},
},
plugins: ['react', 'react-hooks', 'flowtype', 'import'],
globals: {
// fix for eslint-plugin-flowtype/384 not supporting wildcard
_: 'readonly'
},
plugins: ['react', 'react-hooks', 'import'],
rules: {
'no-shadow': ['error'],
indent: ['off'],
'linebreak-style': ['off'],
quotes: ['off'],
semi: ['off'],
'prettier/prettier': ['warn'],
'react/no-direct-mutation-state': ['off'],
'react/display-name': ['off'],
'react-hooks/rules-of-hooks': ['error'],
'react-hooks/exhaustive-deps': ['warn'],
'flowtype/generic-spacing': ['off'],
},

overrides: [
{
// Flow specific rules
files: ['src/index.js.flow', '*/*flow.js', 'examples/*-flow/*/*.js'],
extends: ['plugin:flowtype/recommended'],
plugins: ['flowtype'],
rules: {
'flowtype/generic-spacing': ['off'],
},
},
{
// TypeScript specific rules
files: ['*.{ts,tsx}'],
extends: [
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-empty-interface': 'off',
},
},
{
// Jest env
files: ['*.test.{js,ts,tsx}'],
env: {
jest: true,
},
},
],
};
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
node_modules
lib
coverage
yarn.lock
yarn.lock

# misc
.vscode
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
printWidth: 80,
tabWidth: 2,
semi: true,
singleQuote: true,
trailingComma: 'es5',
}
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
* **Recipes**

- [Flow types](/recipes/flow.md)
- [Typescript types](/recipes/typescript.md)
- [Composition](/recipes/composition.md)
1 change: 1 addition & 0 deletions docs/recipes/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Recipes

- [Flow types](./flow.md)
- [Typescript types](./typescript.md)
- [Composition](./composition.md)
104 changes: 104 additions & 0 deletions docs/recipes/typescript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
## Typing sweet-state with Typescript

This is a basic example:

```ts
import {
createStore,
createSubscriber,
createHook,
createContainer,
ActionApi,
} from 'react-sweet-state';

type State = { count: number };
type Actions = typeof actions;

const initialState: State = {
count: 0,
};

const actions = {
increment: (by = 1) => ({ setState, getState }: ActionApi<State>) => {
setState({
count: getState().count + by,
});
},
};

const Store = createStore<State, Actions>({
initialState,
actions,
});

const CounterSubscriber = createSubscriber(Store);
const useCounter = createHook(Store);
const CounterContainer = createContainer(Store);
```

You don't have to manually type all the `create*` methods, as they can be inferred for the simpler use cases.

#### Actions patterns

If your actions require `Container` props:

```ts
type ContainerProps = { multiplier: number };

const actions = {
increment: (by = 1) => (
{ setState, getState }: ActionApi<State>,
{ multiplier }: ContainerProps
) => {
setState({ count: getState().count + by * multiplier });
},
};
```

#### createSubscriber / createHook patterns

If you provide a selector to your components, you need to defined two additional flow arguments to `createSubscriber`/`createHook`: the selector output and the selector props.

```js
type SelectorState = boolean;
const selector = (state: State): SelectorState => state.count > 0;

// this component does not accept props
const CounterSubscriber = createSubscriber<State, Actions, SelectorState, void>(Store, {
selector
});

// this hook does not accept arguments
const useCounter = createHook<State, Actions, SelectorState, void>(Store, {
selector
});
```

In case your component/hook needs also some props, you can define them as fourth argument:

```js
type SelectorProps = { min: number };
type SelectorState = boolean;
const selector = (state: State, props: SelectorProps): SelectorState => state.count > props.min;

// this component requires props
const CounterSubscriber = createSubscriber<State, Actions, SelectorState, SelectorProps>(Store, {
selector
});

// this hook requires an argument
const useCounter = createHook<State, Actions, SelectorState, SelectorProps>(Store, {
selector
});
```

#### createContainer patterns

If your container requires additional props:

```js
type ContainerProps = { multiplier: number };

// this component requires props
const CounterContainer = createContainer<State, Actions, ContainerProps>(Store);
```
4 changes: 2 additions & 2 deletions examples/basic-flow/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
type Action,
} from 'react-sweet-state';

type State = {
type State = {|
count: number,
};
|};

type Actions = typeof actions;

Expand Down
33 changes: 33 additions & 0 deletions examples/basic-ts/components.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
createStore,
createSubscriber,
createHook,
ActionApi,
} from 'react-sweet-state';

type State = {
count: number;
};

type Actions = typeof actions;

const initialState: State = {
count: 0,
};

const actions = {
increment: () => ({ setState, getState }: ActionApi<State>) => {
setState({
count: getState().count + 1,
});
},
};

const Store = createStore({
initialState,
actions,
});

export const CounterSubscriber = createSubscriber(Store);

export const useCounter = createHook(Store);
23 changes: 23 additions & 0 deletions examples/basic-ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Advanced example with TypeScript</title>
<style>
body {
font-family: sans-serif;
}
main {
display: flex;
line-height: 1.5;
}
hr {
margin: 1em;
}
</style>
</head>

<body>
<div id="root"></div>
<script src="./bundle.js" type="text/javascript"></script>
</body>
</html>
46 changes: 46 additions & 0 deletions examples/basic-ts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// @flow
import React from 'react';
import ReactDOM from 'react-dom';

import '@babel/polyfill';

import { CounterSubscriber, useCounter } from './components';

const CounterHook = () => {
const [{ count }, { increment }] = useCounter();
return (
<div>
<h3>With Hooks</h3>
<p>{count}</p>
<button onClick={increment}>+1</button>
</div>
);
};

const CounterRpc = () => (
<CounterSubscriber>
{({ count }, { increment }) => (
<div>
<h3>With Render-props</h3>
<p>{count}</p>
<button onClick={increment}>+1</button>
</div>
)}
</CounterSubscriber>
);

/**
* Main App
*/
const App = () => (
<div>
<h1>Simple counter example</h1>
<main>
<CounterRpc />
<hr />
<CounterHook />
</main>
</div>
);

ReactDOM.render(<App />, document.getElementById('root'));
Loading