[TypeScript] Functions which do not use this
should be typed as arrow functions
#4860
Open
1 of 5 tasks
Labels
👕 TypeScript
TypeScript typings issue
What happened?
Background
In our project, we have the ESLint rule "@typescript-eslint/unbound-method" enabled. This rule warns us, when we use non-arrow functions without their context, because in those functions
this
might be called, unknowingly from the developer. This happens, e.g. when you destructure an object:So to prevent unwrapping functions which are bound to an object, this rule is very helpful.
Issue
However, when destructuring
useFieldArray
according to the VeeValidate documentation, the same error warning occured.After a look at the source code, I saw that none of the inner functions accessed context with
this
. So the code worked (of course). However when looking at the type definition ofFieldArrayContext
I realized, that the functions are declared as normal functions. It's nothing wrong with the definitions, however, normal function types include the assumption that those functions might access context withthis
and therefore, destructuring is marked as unsafe.Solution proposal
Since the functions do not access context, arrow function types can be used too:
This would represent the code more exact, because the functions are also declared independently from a context. This could also be changed for other type declarations too.
Reproduction steps
useFieldArray
(e.g.const { fields, remove, move } = useFieldArray<SomeType>("path");
)Version
Vue.js 3.x and vee-validate 4.x
What browsers are you seeing the problem on?
Relevant log output
No response
Demo link
https://codesandbox.io/p/devbox/friendly-haslett-rqvyc5?workspaceId=88e64305-8239-47e1-bc7e-8e2730ac554d
Code of Conduct
The text was updated successfully, but these errors were encountered: