-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: use arrow function types over bound functions #12955
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
fix: use arrow function types over bound functions #12955
Conversation
Using class method function types causes SvelteKit users to get TypeScript ESLint errors if they're using the [@typescript-eslint/unbound-method][1] rule (included by default in the `recommended-type-checked` config). [1]: https://typescript-eslint.io/rules/unbound-method/
🦋 Changeset detectedLatest commit: 743d807 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
After a quick find operation on the |
I'm for it, but I was a bit reluctant since it's difficult to know which functions should be bound and can't be safely destructured. So instead I only changed the places that the If you look at my original PR (#12915) where I've also enabled the Unfortunately, I don't think there's an easy ESLint rule to ensure you stay on one standard or the other, other than potential TypeScript/ |
I think maybe the correct thing to do is to inspect the function definitions and use arrow function for functions that don't reference |
|
I'm not much of a TypeScript expert, but that sounds reasonable to me |
|
Are you able to look into it, @aloisklink? |
Convert the rest of the public types in `src/exports/public.d.ts` to use arrow function types instead of bound function types. I kept the following types in `public.d.ts` using non-arrow functions, since - These functions are for user inputs, which might rely on these functions being bound, and so it might be a breaking change to change these types: - [`Emulator['platform']`][1] - functions in `KitConfig` - Class methods that rely on `this`: - [`Server`][2] [1]: https://github.com/sveltejs/kit/blob/b25f2d052bbf22e832ac57cbf9e12c48ac922f96/packages/kit/src/exports/public.d.ts#L272-L276 [2]: https://github.com/sveltejs/kit/blob/b25f2d052bbf22e832ac57cbf9e12c48ac922f96/packages/kit/src/exports/public.d.ts#L1174-L1178
|
In 743d807, I've updated the rest of the I kept the following types in
I also had to make some changes to Also, I found #7396 which did the opposite of this! I still think arrow functions are better so that users can destructure the functions from objects when necessary, but @ignatiusmb (the author of that PR) brought up some reasons why the current function syntax is better (maybe it could be used internally?).
Sorry for the late update! I wanted to get it done last weekend, but I didn't get the time! |
|
Any thoughts on the latest update, @benmccann and @eltigerchino? |
dummdidumm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!
Using class method function types causes SvelteKit users to get TypeScript ESLint errors if they're using the @typescript-eslint/unbound-method rule (included by default in the
recommended-type-checkedconfig).For example:
Switching SvelteKit's public types to arrow functions fixes #12622.
Implementation decisions
I haven't enabled the
@typescript-eslint/unbound-methodrule in this PR. However, I've got a separate PR (#12915) where we enable this rule and we fix internal types as well.I kept the following types in
public.d.tsusing non-arrow functions, since:Emulator['platform']KitConfigthis:ServerI also had to make some changes to
packages/kit/src/runtime/server/cookie.jsto make TypeScript happy.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
parentin load functions receive@typescript-eslint/unbound-methoderror #12622However, I've split it up on the advice of @benmccann (see fix: use arrow functions over bound funcs #12915 (comment))
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits