type/no-unnecessary-type-assertion
Proibir asserções de tipo que não mudam o tipo da expressão.
| Severidade padrão | Predefinições | Correção automática | Opções |
|---|---|---|---|
warning |
nuxt, opinionated |
Não | Opções das regras |
Aplicável a: Informações de tipos em scripts e templates de SFCs Vue, para as construções mostradas abaixo.
Esta regra lê informações de tipo do seu projeto TypeScript (tsconfig.json), por isso a configuração define typeAware: true. typeAware sozinho não ativa regras opcionais.
Incorreto
known já é number, então afirmar number não muda o seu tipo.
<script setup lang="ts">
const known: number = 1;
const asserted = known as number;
</script>
Correto
Usar known sem asserção mantém o tipo number anotado.
<script setup lang="ts">
const known: number = 1;
const asserted = known;
</script>
Configuração (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"type/no-unnecessary-type-assertion": "warn"
},
"typeAware": true
},
});
Depois, execute vp run lint.