piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 98 forks source link

investigate "strictFunctionTypes" issue #44

Closed piotrwitek closed 5 years ago

piotrwitek commented 6 years ago

investigate an issue mentioned here: https://github.com/piotrwitek/typesafe-actions/issues/43#issuecomment-391305184

piotrwitek commented 6 years ago

I think it might be related to #42, I'm planning to investigate it this week.

toolness commented 6 years ago

Hi, I just noticed the same problem that was mentioned in https://github.com/piotrwitek/typesafe-actions/issues/43#issuecomment-391305184.

Specifically, a "default" typescript configuration created via tsconfig --init sets strict: true, which means that strictFunctionTypes is enabled by default. So it's unforunate that the .map() example in the README doesn't work with it.

Here's a full example that fails to compile, which depends only on typesafe-actions (no need for redux, react-redux, etc):

import { createStandardAction } from 'typesafe-actions';

const ADD = '@prefix/ADD';

interface Todo {
  title: string;
  id: string;
  completed: boolean;
}

function cuid(): string {
  return 'this is a fake value';
}

// Below is literally what's in README.md.
export const add = createStandardAction(ADD).map(
  ({ title }: { title: string }) => ({
    payload: { title, id: cuid(), completed: false } as Todo,
  })
);

This is the error output by tsc:

error TS2345: Argument of type '({ title }: { title: string; }) => { payload: Todo; }' is not assignable to parameter of type '(payload?: { title: string; } | undefined, meta?: void | undefined) => { payload: Todo; }'.
  Types of parameters '__0' and 'payload' are incompatible.
    Type '{ title: string; } | undefined' is not assignable to type '{ title: string; }'.
      Type 'undefined' is not assignable to type '{ title: string; }'.

Anyways, for now I am just using createAction for such use cases instead of createStandardAction, e.g. rewriting the above as:

export const add = createAction(ADD, resolve => {
  return ({ title }: { title: string }) => resolve({
    title,
    id: cuid(),
    completed: false
  } as Todo);
});

Regardless, thanks again for making this library, I think it will make my redux life much easier!

IssueHuntBot commented 6 years ago

@boostio funded this issue with $10. Visit this issue on Issuehunt

IssueHuntBot commented 6 years ago

@loadbalance-sudachi-kun funded this issue with $256. Visit this issue on Issuehunt

IssueHuntBot commented 6 years ago

@piotrwitek has started working. Visit this issue on Issuehunt

IssueHuntBot commented 5 years ago

@piotrwitek has submitted a pull request. See it on IssueHunt

IssueHuntBot commented 5 years ago

@piotrwitek has rewarded $186.20 to @piotrwitek. See it on IssueHunt