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

Incorrect type inference with currying #831

Open
3 tasks done
sarmong opened this issue Jul 17, 2021 · 2 comments
Open
3 tasks done

Incorrect type inference with currying #831

sarmong opened this issue Jul 17, 2021 · 2 comments

Comments

@sarmong
Copy link

sarmong commented Jul 17, 2021

🐛 Bug Report

When using currying, produce doesn't return the original type

Link to repro

https://codesandbox.io/s/immer-sandbox-forked-342jd?file=/src/index.ts

To Reproduce

Use produce function without data and check the return type.

Observed behavior

Curried produce returns:

const updatedCache2: (oldAssignments: Assignment[]) => <Base extends readonly {
    readonly assignmentBk: string;
    readonly contentBk: string;
    readonly createdDate: Date;
    readonly startDate: Date;
    readonly dueDate: Date;
    readonly done: boolean;
}[]>(base?: Base) => Base

Expected behavior

Curried produce returns the original type.

I need it, because it fails when using with Ramda's pipe function.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version: 8.0.1
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@childrentime
Copy link
Contributor

For the function updateCache2 , arguments will be narrow to Immutable<Assignment[]>. If we don't want this, we can specify the generic state. However, type of function updatedCache3 is (state: Assignment[] | undefined) => Assignment[] .Maybe this '?' can be removed?

<State>(
  recipe: (
	  state: Draft<State>,
	  initialState: State
              ) => ValidRecipeReturnType<State>
): (state?: State) => State

@childrentime
Copy link
Contributor

it seems like this function

const updatedCache3 = produce<Assignment[]>((draft) => {
  const updatedAssignment = draft.find(
    ({contentBk}) => contentBk === assignment.contentBk
  )
  if (updatedAssignment) {
    updatedAssignment.done = !updatedAssignment.done
  }
})

will map to

 <State>(recipe: (state: Draft<State>, initialState: State) => ValidRecipeReturnType<State>): (state?: State) => State;

not map to

  <State>(recipe: (state: Draft<State>) => ValidRecipeReturnType<State>): (state: State) => State;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants