Vize

type/prefer-reduce-type-parameter

Preferir o parâmetro de tipo de Array#reduce a uma asserção do valor inicial.

Severidade padrão Predefinições Correção automática Opções
warning nuxt, opinionated Não Nenhuma

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

O valor inicial é afirmado como number[] em vez de usar o parâmetro de tipo de reduce.

<script setup lang="ts">
const numbers = [1, 2, 3];
const collected = numbers.reduce((all, n) => [...all, n], [] as number[]);
</script>

Correto

reduce<number[]> declara o tipo do acumulador sem afirmar o array inicial.

<script setup lang="ts">
const numbers = [1, 2, 3];
const collected = numbers.reduce<number[]>((all, n) => [...all, n], []);
</script>

Configuração (Vite+)

import { defineConfig } from "@vizejs/vite-plugin/vite-plus";

export default defineConfig({}).withVue({
  linter: {
    "preset": "incremental",
    "rules": {
      "type/prefer-reduce-type-parameter": "warn"
    },
    "typeAware": true
  },
});

Depois, execute vp run lint.

Páginas relacionadas