Skip to content

Support for type-checked ESLint rules when importing from .svelte filesΒ #16264

@Stadly

Description

@Stadly

Describe the problem

ESLint is not able to run type-checked rules on values imported from .svelte files.

For example:

<!-- baz.svelte -->
<script lang="ts" module>
  export const baz = 1;
</script>
// test.ts
import { baz } from './baz.svelte';
const c = baz + 1;

Will give the following error when running ESLint with the recommended type-checked rules on test.ts:

  2:7  error  Unsafe assignment of an error typed value  @typescript-eslint/no-unsafe-assignment

There is a package typescript-eslint-parser-for-extra-files that aims to solve this issue, but I cannot get it to work.

Is there anything Svelte can do to help with this issue?

Describe the proposed solution

I have some ideas, but not sure if they are viable:

  1. Get typescript-eslint-parser-for-extra-files to work. Maybe there is just a configuration issue? I've done my best to try everyhting, but if someone knows how to do it then problem solved :)
  2. The Svelte community gets together to fix typescript-eslint-parser-for-extra-files or fix the issue in some other way. Maybe it can be fixed as part of eslint-plugin-svelte or svelte-eslint-parser?
  3. Let Svelte generate some code providing the types. For example, the error disappears when I have the following code in my code base. But not sure if this can be done in a way that works regardles of the import path (this will only fix it when importing from './baz.svelte', not from for example '$lib/baz.svelte').
declare module './baz.svelte' {
  export const baz: number;
}
  1. Don't export anything from .svelte files. Instead create a sibling .ts file with the export and import in into both the .svelte file and the other files. I don't think this will always be possible or viable, for example it would at best be cumbersome with snippets using createRawSnippet.
  2. Somehow disable the type checked rules on values imported from .svelte files.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions