Skip to content

If component is generic, events and slot bindings are treated as any in ESLint  #564

Closed
@ptrxyz

Description

@ptrxyz

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

  • I'm using eslint-plugin-svelte. (*.svelte file linting does not work with the parser alone. You should also use eslint-plugin-svelte with it.)
  • I'm sure the problem is a parser problem. (If you are not sure, search for the issue in eslint-plugin-svelte repo and open the issue in eslint-plugin-svelte repo if there is no solution.
  • 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?

9.10.0

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

  • svelte-eslint-parser@0.41.1
  • eslint-plugin-svelte@2.44.0

What did you do?

Configuration
import js from '@eslint/js';
import ts from 'typescript-eslint';
import svelte from 'eslint-plugin-svelte';
import prettier from 'eslint-config-prettier';
import globals from 'globals';

/** @type {import('eslint').Linter.Config[]} */
export default [
	js.configs.recommended,
	...ts.configs.recommendedTypeChecked,
	...svelte.configs['flat/recommended'],
	prettier,
	...svelte.configs['flat/prettier'],
	{
		languageOptions: {
			parserOptions: {
				parser: ts.parser,
				project: ['./tsconfig.json']
			},
			globals: {
				...globals.browser,
				...globals.node
			}
		}
	},
	{
		files: ['**/*.svelte'],
		languageOptions: {
			parser: svelte.parser,
			parserOptions: {
				parser: ts.parser,
				project: ['.svelte-kit/tsconfig.json', './tsconfig.json'],
				extraFileExtensions: ['.svelte']
			}
		}
	},
	{
		ignores: ['build/', '.svelte-kit/', 'dist/']
	}
];

Consider this code:

<script lang="ts">
    // main.svelte
    import type { ComponentEvents } from 'svelte';
    import Comp from './component.svelte';

    type X = ComponentEvents<Comp>['foo'];
    export const x: X | null = null;
</script>
<script lang="ts">
    // component.svelte 
    import { createEventDispatcher } from 'svelte';

    export const dispatch = createEventDispatcher<{
        foo: { id: number; name: string };
    }>();
</script>

As you can see X should be typed as an object, which is correctly detected by VSCode:
image

However when running eslint with type-checking rules enabled, eslint produces an error indicating, that X is simply any:
image

Further investigation with more rules that check for types (i.e. @typescript-eslint/no-unsafe-assignment and @typescript-eslint/no-unsafe-member-access) confirm that for eslint, X is simply any

What did you expect to happen?

I wanted eslint to not throw the error since X is properly typed.

What actually happened?

eslint throws an error indicating that X is simply any.

Link to GitHub Repo with Minimal Reproducible Example

https://github.com/ptrxyz/bug-svelte-eslint-componentevents/tree/main

Additional comments

No response

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