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

TS Error: finishDraft Expected 2 arguments, but got 1. #492

Closed
eddyw opened this issue Dec 23, 2019 · 3 comments
Closed

TS Error: finishDraft Expected 2 arguments, but got 1. #492

eddyw opened this issue Dec 23, 2019 · 3 comments
Labels

Comments

@eddyw
Copy link

eddyw commented Dec 23, 2019

🐛 Bug Report

TS complains that the second argument for finishDraft is required.

To Reproduce

const final = finishDraft(draft)

Observed behavior

Our build started to fail with latest version of immer:

TS2554: Expected 2 arguments, but got 1.

96     const cssAttributesFinal = finishDraft(cssAttributes)
                                  ~~~~~~~

And it's everywhere. There is no update in the docs that the patch listener has become a required argument, so I think this is a bug with typings of the function.

Expected behavior

The second argument for finishDraft should be optional.

Environment

  • Immer version: 5.0.1
@mweststrate
Copy link
Collaborator

mweststrate commented Dec 23, 2019 via email

@CGamesPlay
Copy link

To save anyone who investigates this some time, the following is not a valid fix. You would think this would work, however after applying this patch, immer.d.ts is correct, however index.d.ts file ends up incorrect.

diff --git i/src/immer.ts w/src/immer.ts
index a6d94cf..6457493 100644
--- i/src/immer.ts
+++ w/src/immer.ts
@@ -178,7 +178,7 @@ export class Immer implements ProducersFns {
 		return proxy as any
 	}
 
-	finishDraft<D extends Draft<any>>(draft: D, patchListener: PatchListener): D extends Draft<infer T> ? T : never {
+	finishDraft<D extends Draft<any>>(draft: D, patchListener?: PatchListener): D extends Draft<infer T> ? T : never {
 		const state = draft && draft[DRAFT_STATE]
 		if (!state || !state.isManual) {
 			throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore
diff --git i/src/scope.ts w/src/scope.ts
index 4436fea..72d185f 100644
--- i/src/scope.ts
+++ w/src/scope.ts
@@ -24,7 +24,7 @@ export class ImmerScope {
 		this.patches = null as any // TODO:
 	}
 
-	usePatches(patchListener: PatchListener) {
+	usePatches(patchListener?: PatchListener) {
 		if (patchListener) {
 			this.patches = []
 			this.inversePatches = []

This results in an index.d.ts that looks like this:

export declare const finishDraft: <D extends any>(draft: D, patchListener: import("./types").PatchListener | undefined) => D extends import("./types").Draft<infer T> ? T : never;

mweststrate added a commit that referenced this issue Jan 9, 2020
mweststrate added a commit that referenced this issue Jan 9, 2020
@aleclarson
Copy link
Member

🎉 This issue has been resolved in version 5.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

4 participants