Skip to content

Using volar together with @typescript-eslint/parser for type checking rules to understand SFC component types #2189

@DrJume

Description

@DrJume

I'm using @typescript-eslint in a TS project with type checking enabled (https://typescript-eslint.io/linting/configs#recommended-requiring-type-checking).

This allows eslint to understand typescript types and make suggestions.

Sadly Vue SFC files are interpreted with type any, they are not correctly loaded.
I found an option for @typescript-eslint/parser to specify a TS program in the options: (https://typescript-eslint.io/architecture/parser/#program). So it should be possible to pass the Volar proxy into @typescript-eslint/parser. This should allow eslint to also run type linting for Vue SFCs.
But sadly I didn't get it to work.

How can I get a TS program which uses Volar and pass it to @typescript-eslint/parser?

This is my eslint config override for '*.vue' files:

...
overrides: [
    {
        files: ['*.vue'],
        extends: [
            'plugin:@typescript-eslint/recommended-requiring-type-checking',
            'plugin:@typescript-eslint/strict',
        ],
        parser: 'vue-eslint-parser',
        parserOptions: {
            parser: '@typescript-eslint/parser',
            parserOptions: {
                extraFileExtensions: ['.vue'],
                tsconfigRootDir: __dirname,
                project: ['./tsconfig.app.json'],
                program: vueTsc.createProgramProxy({
                    rootNames: ['./src/main.ts'],
                    options: compilerOptions,
                    host: ts.createCompilerHost(compilerOptions),
                }),
            },
        }
    }
]
...

I also tried to add the typescript-vue-plugin as a typescript plugin, to get SFCs to work with eslint type checking, but it also didn't work...

"compilerOptions": {
    "plugins": [
        {
            "name": "typescript-vue-plugin"
        }
    ]
}

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