Skip to content

vue-perfectionist/no-template-type-assertion ​

Disallow as type assertions in Vue templates.

At a glance

ConcernBehavior
ScopeTemplate
DefaultsReport every as assertion, including as const; move it into scripts.
Required optionsNone; enabling the rule uses its defaults.
Automatic fixes—
Type informationNo TypeScript type services required.

Usage ​

Configure vue-eslint-parser with @typescript-eslint/parser to parse TypeScript expressions in templates. No TypeScript type services are required.

js
import tsParser from '@typescript-eslint/parser'
import vuePerfectionist from 'eslint-plugin-vue-perfectionist'
import vueParser from 'vue-eslint-parser'

export default [
  {
    files: ['**/*.vue'],
    languageOptions: {
      parser: vueParser,
      parserOptions: { parser: tsParser },
    },
    plugins: { 'vue-perfectionist': vuePerfectionist },
    rules: { 'vue-perfectionist/no-template-type-assertion': 'error' },
  },
]

📖 Rule Details ​

Move type assertions into <script> to keep templates focused on rendering. Prefer accurate source types or type guards when possible. When an assertion is necessary, use a computed property to preserve reactivity, or a function that accepts template-local values.

vue
<script setup lang="ts">
import { computed } from 'vue'

const props = defineProps<{ value: unknown }>()
const label = computed(() => props.value as string)
</script>

<template>
  <AppLabel :value="label" />
  <span>{{ label }}</span>
</template>
correct
vue
<script setup lang="ts">
defineProps<{ value: unknown }>()
</script>

<template>
  <AppLabel :value="
value as string
" />
<span>{{
value as string
}}</span>
</template>
incorrect

Constant assertions and chained assertions are also reported. Each as assertion receives its own diagnostic.

vue
<script setup lang="ts">
defineProps<{ value: unknown }>()
</script>

<template>
  <AppLabel :value="
'ready' as const
" />
<span>{{
value as unknown as string
}}</span>
</template>
incorrect

🔧 Options ​

This rule has no options.

Scope ​

Checks every parsed TSAsExpression in Vue templates, including interpolations, directive values, event handlers, v-for sources, slot defaults, and dynamic directive arguments. Nested assertions in callbacks and other expressions are also checked. Assertions referencing v-for or slot variables are reported; move them into a function accepting those variables or into a child component.

Script blocks and standalone JavaScript/TypeScript files are not checked. Static text, static attributes, comments, v-pre content, and expressions the parser cannot parse are skipped. This rule does not prohibit satisfies, non-null assertions (value!), or angle-bracket type assertions (<Type>value).

No automatic fix or suggestion is provided. Extraction requires choosing a name and preserving reactivity and template-local scope.

Alternative: vue/no-restricted-syntax ​

If you already use eslint-plugin-vue, you can use vue/no-restricted-syntax with the TSAsExpression selector instead. It checks template expressions and supports a custom message:

js
import tsParser from '@typescript-eslint/parser'
import vue from 'eslint-plugin-vue'
import vueParser from 'vue-eslint-parser'

export default [
  {
    files: ['**/*.vue'],
    languageOptions: {
      parser: vueParser,
      parserOptions: { parser: tsParser },
    },
    plugins: { vue },
    rules: {
      'vue/no-restricted-syntax': [
        'error',
        {
          selector: 'TSAsExpression',
          message: 'Move this type assertion into <script>.',
        },
      ],
    },
  },
]

Enable either this rule or the alternative to avoid duplicate diagnostics.

🔍 Implementation ​