kit icon indicating copy to clipboard operation
kit copied to clipboard

`parent` in load functions receive `@typescript-eslint/unbound-method` error

Open rakuzen25 opened this issue 1 year ago • 0 comments

Describe the bug

My +page.ts looks like something like this:

export const load: PageLoad = async ({ fetch, parent }) => {
    const { uid } = (await parent()).session?.user ?? {};
    if (!uid) {
        throw new TypeError("User ID not found");
    }

    // Do something
};

However, eslint seems not very happy about the parent function reference and throws

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)

Docs on unbound-method

Reproduction

MRE on Stackblitz

Run pnpm eslint ./src to see the errors.

Logs

/home/projects/stackblitz-starters-1y7qpk/src/routes/sum/+layout.ts
  3:42  error  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  @typescript-eslint/unbound-method

/home/projects/stackblitz-starters-1y7qpk/src/routes/sum/+page.ts
  3:40  error  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  @typescript-eslint/unbound-method

✖ 2 problems (2 errors, 0 warnings)

System Info

System:
  OS: Linux 5.0 undefined
  CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Memory: 0 Bytes / 0 Bytes
  Shell: 1.0 - /bin/jsh
Binaries:
  Node: 18.20.3 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 10.2.3 - /usr/local/bin/npm
  pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
  @sveltejs/adapter-auto: ^3.0.0 => 3.2.4 
  @sveltejs/kit: ^2.0.0 => 2.5.25 
  @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.2 
  svelte: ^4.2.7 => 4.2.19 
  vite: ^5.0.3 => 5.4.2

Severity

annoyance

Additional Information

Patrick in the server mentioned that perhaps https://github.com/sveltejs/kit/blob/45cb8c5f305b4d9e61a32e19af8059dc0da4c255/packages/kit/types/index.d.ts#L776-L782

and https://github.com/sveltejs/kit/blob/45cb8c5f305b4d9e61a32e19af8059dc0da4c255/packages/kit/types/index.d.ts#L1201-L1206

needs to be changed to parent(this: void): Promise<ParentData>; or parent: () => Promise<ParentData>;, but it seems like this file is auto-generated…

rakuzen25 avatar Aug 28 '24 03:08 rakuzen25