-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: main
Are you sure you want to change the base?
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! |
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-checked
config).For example:
Switching SvelteKit's public types to arrow functions fixes #12622.
Implementation decisions
I haven't enabled the
@typescript-eslint/unbound-method
rule 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.ts
using non-arrow functions, since:Emulator['platform']
KitConfig
this
:Server
I also had to make some changes to
packages/kit/src/runtime/server/cookie.js
to make TypeScript happy.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
parent
in load functions receive@typescript-eslint/unbound-method
error #12622However, I've split it up on the advice of @benmccann (see fix: use arrow functions over bound funcs #12915 (comment))
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits