Skip to content

[v1.10.3] Immutable<T> broke my codebase #289

@knpwrs

Description

@knpwrs

Hey, there! I'm the guy that provided the original mapped type for removing readonly modifiers in TypeScript over in #161.

It's awesome to see this library gain so much traction since then, though the typings have also evolved to the point where I can barely recognize them. The latest patch release of immer broke a library I wrote (my tests pass using [email protected] but not [email protected]). It appears to be purely a typing thing, but I'm having a heck of a time resolving the issues. I was hoping somebody who has worked on the typings more recently than I have could give some pointers/feedback/maybe a PR against my project (or maybe we might determine that the typings are broken and not my usage and then we can fix the problem here).

The issue Greenkeeper opened against my repo can be seen here: knpwrs/redux-ts-utils#1

This is the code that was working with [email protected]:

export default function handleAction<S, AC extends TsActionCreator<any> = any>(
  ac: AC,
  re: TsReducer<S, ReturnType<AC>>,
  s?: S,
): Reducer<S, ReturnType<AC>> {
  return produce((draft: Draft<S>, action: ReturnType<AC>) => {
    if (action.type === ac.type) {
      re(draft, action);
    }
  }, s);
}

Lots of red all over the place. Here's where I've gotten so far with [email protected]:

export default function handleAction<S, AC extends TsActionCreator<any> = any>(
  ac: AC,
  re: TsReducer<S, ReturnType<AC>>,
  s?: S,
): Reducer<S, ReturnType<AC>> {
  return produce<S, Draft<S>, [ReturnType<AC>]>((draft, action) => {
    if (action.type === ac.type) {
      re(draft, action);
    }
  }, s);
}

Now the only thing that is red is that second-to-last line where I'm passing s as the default value to produce. TypeScript complains:

Argument of type 'S | undefined' is not assignable to parameter of type 'S'.
  Type 'undefined' is not assignable to type 'S'.

Nothing I'm trying is resolving the issue (even type casting or the dreaded ! operator which results in more errors). I want s to remain an optional parameter.

Any ideas?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions