Skip to content
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

typescript-eslint's "no any" rules don't work with inferred function types from sveltekit #413

Open
2 tasks done
DetachHead opened this issue Mar 22, 2023 · 13 comments
Open
2 tasks done
Labels

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Mar 22, 2023

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.

What version of ESLint are you using?

8.33.0

What version of eslint-plugin-svelte are you using?

2.22.0

What did you do?

Configuration
{
    "rules": {
        "@typescript-eslint/no-unsafe-member-access": "error"
    }
}
// routes/api/+server.ts
export const GET = async ({ request }) => {
    // request is not any, since it's inferred as Request by sveltekit
    request.headers // error: @typescript-eslint/no-unsafe-member-access
}

What did you expect to happen?

no error

What actually happened?

@typescript-eslint/no-unsafe-member-access error

Link to GitHub Repo with Minimal Reproducible Example

DetachHead/eslint-plugin-svelte-repros@7bf800d

Additional comments

originally raised at #390 (comment)

@ota-meshi
Copy link
Member

Where are the types for GET sourced from? I don't understand the code you provided because there are no type declarations. Could you please share a repository to reproduce the issue?
I've only made static sites using svelte kit, so I'm not sure 😓

@ota-meshi ota-meshi added needs repro Need a repository that can reproduce the problem feat: typescript labels Apr 3, 2023
DetachHead added a commit to DetachHead/eslint-plugin-svelte-repros that referenced this issue Apr 3, 2023
@DetachHead
Copy link
Contributor Author

DetachHead commented Apr 3, 2023

sveltekit infers the types so you don't have to annotate them explicitly. i've updated the issue with a github repo. to reproduce the error, checkout that branch then run the following commands

npm install
npm run check
npx eslint .

@polyzen
Copy link
Contributor

polyzen commented Apr 3, 2023

Think you mean npx eslint .

@DetachHead
Copy link
Contributor Author

DetachHead commented Apr 3, 2023

true. i added ./node_modules/.bin to my path so i forgot about npx. updated my comment

@polyzen
Copy link
Contributor

polyzen commented Apr 3, 2023

Inferring types is a new feature: https://svelte.dev/blog/zero-config-type-safety

@ota-meshi ota-meshi removed the needs repro Need a repository that can reproduce the problem label Apr 3, 2023
@ota-meshi
Copy link
Member

Thank you for the repo to reproduce.
It seems that the svelte language server and svelte-check assign their own types. (Because raw tsc command fails.)
I think we should check how it is implemented 👀.

@ota-meshi ota-meshi added the enhancement New feature or request label Apr 3, 2023
@ota-meshi
Copy link
Member

@ota-meshi
Copy link
Member

Hmm.. We may need new typescript parser for the eslint (not svelte parser). We should check if it integrates well with svelte2tsx.

@jasonpanosso
Copy link

I still have this issue, is there any progress on this, or any known workaround/solution?

@thenbe
Copy link

thenbe commented Jun 16, 2024

Another minimal repro here: https://github.com/thenbe/repro-eslint-svelte-typecheck

Reproduction steps

  1. Clone and pnpm install
  2. Run: pnpm eslint src/routes/+page.svelte

Expected: We should get no eslint errors.
Actual: We get two eslint errors. Error message:

src/routes/+page.svelte
   7:20  error  Unsafe member access .url on an `any` value       @typescript-eslint/no-unsafe-member-access
  11:8   error  Unsafe member access .greeting on an `any` value  @typescript-eslint/no-unsafe-member-access

Other

svelte-check (i.e. pnpm run check) passes without errors. Only eslint shows error.

@ryoppippi
Copy link

ryoppippi commented Jun 28, 2024

Got the same issue....

So,

export async function load({ fetch }) {
	return await fetch('/foo');
}

this causes an error because fetch is inferred as any in eslint.
However, this works fine for svelte-check and svelte lsp

@keve1227
Copy link

keve1227 commented Jul 29, 2024

A workaround is to import the offending type explicitly:

import type { RequestEvent } from "./$types";

export async function GET({ url }: RequestEvent) {
    return new Response(url.searchParams.get("message")); // no errors
}

Which gives correct linting.

@thenbe
Copy link

thenbe commented Jul 29, 2024

A workaround is to import the offending type explicitly

I wasn't sure how to apply this workaround to the minimal repro posted in the comment above.

The issue that remains is that eslint fails to see the inferred types not only in the +page.ts file, but in its sibling +page.svelte as well.

In other words, eslint does not play nice with sveltekit's zero-config-type-safety.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants