Skip to content

Type exports from <script module> in .svelte files resolve as "error" types when used in combination type expressions (unions, intersections) across files #915

@glypse

Description

@glypse

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?

10.4.1

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

  • svelte-eslint-parser@1.8.0
  • eslint-plugin-svelte@3.19.0

What did you do?

Configuration
// eslint.config.js
import prettier from 'eslint-config-prettier';
import path from 'node:path';
import js from '@eslint/js';
import svelte from 'eslint-plugin-svelte';
import { defineConfig, includeIgnoreFile } from 'eslint/config';
import globals from 'globals';
import ts from 'typescript-eslint';

const gitignorePath = path.resolve(import.meta.dirname, '.gitignore');

export default defineConfig(
	includeIgnoreFile(gitignorePath),
	js.configs.recommended,
	ts.configs.strictTypeChecked,
	ts.configs.stylisticTypeChecked,
	svelte.configs.recommended,
	prettier,
	svelte.configs.prettier,
	{
		languageOptions: {
			globals: { ...globals.browser, ...globals.node },
			parserOptions: {
				projectService: true
			}
		},
		rules: {
			'no-undef': 'off'
		}
	},
	{
		files: ['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js'],
		languageOptions: {
			parserOptions: {
				projectService: true,
				extraFileExtensions: ['.svelte', '.svelte.ts', '.svelte.ts'],
				parser: ts.parser
			}
		}
	},
	{
		rules: {}
	}
);
<!-- GenericSource.svelte -->
<script module lang="ts">
	export type myType = string;
</script>
<!-- GenericConsumer.svelte -->
<script lang="ts">
	import type { myType } from './GenericSource.svelte';

	// 'myType' is an 'error' type that acts as 'any' and overrides all other types in this union type  @typescript-eslint/no-redundant-type-constituents
	type myOtherType = myType | string;
</script>

What did you expect to happen?

myType to resolve as string

What actually happened?

'myType' is an 'error' type that acts as 'any' and overrides all other types in this union type @typescript-eslint/no-redundant-type-constituents

Link to GitHub Repo with Minimal Reproducible Example

https://github.com/glypse/svelteEslintTsScriptModuleBugRepro

Additional comments

  1. A .svelte file that export types something from its <script module> works fine when the consumer uses it directly (type X = ImportedType).
  2. If the consumer uses that same imported type in a union (type X = ImportedType | string) or intersection (type X = ImportedType & { ... }), the imported type resolves as an "error" type (collapsed to any), triggering @typescript-eslint/no-redundant-type-constituents.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions