Skip to content

Type annotations not working in Svelte's HTML template section #255

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

Closed
2 tasks done
ptrxyz opened this issue Sep 19, 2022 · 3 comments
Closed
2 tasks done

Type annotations not working in Svelte's HTML template section #255

ptrxyz opened this issue Sep 19, 2022 · 3 comments

Comments

@ptrxyz
Copy link
Contributor

ptrxyz commented Sep 19, 2022

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.23.1

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

2.8.0

What did you do?

Configuration
module.exports = {
	root: true,
	parser: '@typescript-eslint/parser',
	extends: [
		'eslint:recommended',
		'plugin:@typescript-eslint/recommended',
		'plugin:@typescript-eslint/recommended-requiring-type-checking',
		'plugin:import/recommended',
		'plugin:import/typescript',
		'plugin:svelte/recommended',
		'plugin:svelte/prettier',
		'prettier'
	],
	plugins: ['unused-imports', '@typescript-eslint', 'import'],
	ignorePatterns: ['*.cjs', 'svelte.config.js', 'tailwind.config.cjs', 'postcss.config.cjs'],
	overrides: [
		{
			files: ['*.svelte'],
			parser: 'svelte-eslint-parser',
			parserOptions: {
				parser: '@typescript-eslint/parser'
			}
		}
	],
	settings: {
		'svelte3/typescript': () => require('typescript'),
		'import/resolver': { typescript: {} },
		'import/parsers': { '@typescript-eslint/parser': ['.ts', '.tsx'] }
	},
	parserOptions: {
		sourceType: 'module',
		ecmaVersion: 2020,
		project: ['./tsconfig.json'],
		extraFileExtensions: ['.svelte']
	},
	env: {
		browser: true,
		es2017: true,
		node: true
	},
	rules: {
		'no-unused-vars': 'off',
		'@typescript-eslint/no-unused-vars': 'off',
		'unused-imports/no-unused-vars': [
			'warn',
			{ vars: 'all', varsIgnorePattern: '^_', args: 'after-used', argsIgnorePattern: '^_' }
		],
		'unused-imports/no-unused-imports': 'error'
	}
}

<script lang="ts">
// this works:
const something = (e:any) => e
</script>

<!-- this does not -->
<div on:click={(e : any) => e}>Click</div>

What did you expect to happen?

I expect on:click={(e : any) => e} on the div to work.

What actually happened?

eslint complains that the : would be an unexpected token. As soon as I remove the type annotations, things work great.

Parsing error: Unexpected token (eslint)

Link to Minimal Reproducible Example

EXAMPLE

Additional comments

No response

@JounQin
Copy link
Collaborator

JounQin commented Sep 19, 2022

Is that valid syntax for Svelte itself?

@ptrxyz
Copy link
Contributor Author

ptrxyz commented Sep 20, 2022

Hm, good question. 🤔
But I just found this: sveltejs/svelte#5403
Could it be that it's not...?

@ota-meshi
Copy link
Member

Svelte's compiler does not allow TypeScript inside HTML templates.
Please follow sveltejs/svelte#4701.

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

No branches or pull requests

3 participants