jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.91k stars 2.79k forks source link

The type of `setFieldValue` seems incorrect. #3807

Closed NagaiKoki closed 1 year ago

NagaiKoki commented 1 year ago

Bug report

The following Eslint error appeared in the setFieldValue function when updating to version 2.3.2 of formik.

Avoid referencing unbound methods which may cause unintentional scoping of `this`.
If your function does not access `this`, you can annotate it with `this: void`, or consider using an arrow function instead.eslint@typescript-eslint/ unbound-method

When I look at the FormikHelpers types, though, they were defined as follows,

setFieldValue(field: string, value: any, shouldValidate?: boolean): Promise<void | FormikErrors<Values>>;.

However, looking at the implementation, I think the correct type is

setFieldValue: (field: string, value: any, shouldValidate?: boolean) => Promise<void | FormikErrors<Values>>;

Is this type correct?

Current Behavior

スクリーンショット 2023-05-29 14 32 11

export interface FormikHelpers<Values> {
    // ...
    setFieldValue(field: string, value: any, shouldValidate?: boolean): Promise<void | FormikErrors<Values>>;
     // ...
}

Expected behavior

export interface FormikHelpers<Values> {
    // ...
    setFieldValue: (field: string, value: any, shouldValidate?: boolean) => Promise<void | FormikErrors<Values>>;
  // ...
}

Reproducible example

Suggested solution(s)

Additional context

Your environment

Software Version(s)
Formik 2.4.0
React 18.2.0
TypeScript 5.0.0
Browser
npm/Yarn yarn
Operating System
quantizor commented 1 year ago

We can fix this, but it's probably also better to not check types that aren't yours with https://www.typescriptlang.org/tsconfig#skipLibCheck

klarstrup commented 1 year ago

I face this issue with skipLibCheck enabled.

quantizor commented 1 year ago

Fixed in https://github.com/jaredpalmer/formik/releases/tag/formik%402.4.1, thanks for contributing!

NagaiKoki commented 1 year ago

@probablyup Thank you!