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

Improve typings for typescript with generics #41

Merged
merged 3 commits into from
May 10, 2018

Conversation

msuntharesan
Copy link

This way, the store's state type and action type are inferred. This helps with autocomplete in editors.
Here is a sample of how it will be.

import * as React from 'react';
import { initStore, Store } from './dist';

type State = {
  count: number
}

type Actions = {
  increment: () => void,
  incrementBy: (by: number) => void;
}

const store: Store<State, Actions> = {
  initialState: { count: 0 },
  actions: {
    increment: ({ count }) => ({ count: count + 1 }),
    incrementBy: ({ count }, by: number) => ({ count: count + by })
  },
}

const { Provider, connect } = initStore(store)

type CountProps = { count?: number, actions?: Actions };
let Count: React.ComponentType<CountProps> = ({ count, actions }) => (
  <div>
    {count}
    <button onClick={actions.increment}>+</button>
    <button onClick={() => actions.incrementBy(3)}>+3</button>
  </div>
)

Count = connect(state => ({ count: state.count }))(Count)

const App = () => (
  <Provider>
    <Count />
  </Provider>
)

Type of the connect function can be improved though to accept incoming props and merge with props from selector function.

@coveralls
Copy link

coveralls commented Apr 24, 2018

Pull Request Test Coverage Report for Build 30

  • 0 of 0 (NaN%) changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 28: 0.0%
Covered Lines: 40
Relevant Lines: 40

💛 - Coveralls

@didierfranc didierfranc merged commit eb5fcdd into didierfranc:master May 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants