Skip to content

TypeScript type assertion in setup script causes parsing error #177

Closed as not planned
@dmolesUC

Description

@dmolesUC

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

  • I'm using eslint-plugin-vue.
  • I'm sure the problem is a parser problem. (If you are not sure, search for the issue in eslint-plugin-vue repo and open the issue in eslint-plugin-vue 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?

8.32.0

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

What did you do?

Configuration
// .eslintrc.cjs
module.exports = {
    "env": {
        "browser": true,
        "es2021": true
    },
    root: true,
    parser: "vue-eslint-parser",
    parserOptions: {
        parser: "@typescript-eslint/parser",
    },
    extends: [
        "eslint:recommended",
        "@vue/typescript/recommended",
    ],
    plugins: ["@typescript-eslint"],
    rules: {
    },
}
<script setup lang="ts">
import { Ref, ref } from 'vue'

defineProps<{ msg: string }>()

const count: Ref<number | undefined> = ref(0)

function incrementCount() {
  const c = <number>count.value // <- type assertion is here
  count.value = c + 1
}
</script>

What did you expect to happen?

Code lints without errors; or, at least, ESLint is able to parse the Vue component source file.

What actually happened?

Closing brace of the incrementCount() function produces the following parsing error:

/Users/david/Work/Scratch/ts-test/src/components/HelloWorld.vue
  12:0  error  Parsing error: Unexpected token. Did you mean `{'}'}` or `&rbrace;`?

✖ 1 problem (1 error, 0 warnings)

Link to Minimal Reproducible Example

https://github.com/dmolesUC/ts-test

Additional comments

This is the out-of-the box Vite Vue3/Typescript example, slightly modified to add ESLint and the offending code.

Run yarn lint from the project root (or yarn eslint --ext .js,.ts,.vue src).

Relevant code is in src/components/HelloWorld.vue.

Note that identical code in src/helpers/helpers.ts parses & lints without error.

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