Przejdź do głównej zawartości

useReduceTypeParameter

Ta treść nie jest jeszcze dostępna w Twoim języku.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useReduceTypeParameter": "error"
}
}
}
}

Enforce using a type parameter on Array#reduce instead of casting the initial value.

When using Array#reduce, the type of the accumulator is inferred from the initial value. If you use a type assertion (as or angle bracket <T>) on the initial value, the type is not checked against the accumulator usage in the callback. Using a type parameter on reduce instead is more type-safe because TypeScript will verify that the callback’s return type matches the declared type.

const arr: number[] = [1, 2, 3];
arr.reduce((sum, num) => sum.concat(num * 2), [] as number[]);
code-block.ts:2:1 lint/nursery/useReduceTypeParameter  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The initial value of Array#reduce uses a type assertion.

1 │ const arr: number[] = [1, 2, 3];
> 2 │ arr.reduce((sum, num) => sum.concat(num * 2), [] as number[]);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

Type assertions can hide type mismatches in the reducer callback. Use a type parameter on the call instead, so TypeScript checks the return type.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Use a type parameter instead of type assertion.

1 1 const arr: number[] = [1, 2, 3];
2 - arr.reduce((sum,·num)·=>·sum.concat(num·*·2),·[]·as·number[]);
2+ arr.reduce<number[]>((sum,·num)·=>·sum.concat(num·*·2),·[]);
3 3

const arr: string[] = ['a', 'b'];
arr.reduce((acc, name) => ({ ...acc, [name]: true }), {} as Record<string, boolean>);
code-block.ts:2:1 lint/nursery/useReduceTypeParameter  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The initial value of Array#reduce uses a type assertion.

1 │ const arr: string[] = [‘a’, ‘b’];
> 2 │ arr.reduce((acc, name) => ({ …acc, [name]: true }), {} as Record<string, boolean>);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

Type assertions can hide type mismatches in the reducer callback. Use a type parameter on the call instead, so TypeScript checks the return type.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Use a type parameter instead of type assertion.

1 1 const arr: string[] = [‘a’, ‘b’];
2 - arr.reduce((acc,·name)·=>·({·...acc,·[name]:·true·}),·{}·as·Record<string,·boolean>);
2+ arr.reduce<Record<string,·boolean>>((acc,·name)·=>·({·...acc,·[name]:·true·}),·{});
3 3

const arr: number[] = [1, 2, 3];
arr.reduceRight((sum, num) => sum.concat(num * 2), [] as number[]);
code-block.ts:2:1 lint/nursery/useReduceTypeParameter  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The initial value of Array#reduceRight uses a type assertion.

1 │ const arr: number[] = [1, 2, 3];
> 2 │ arr.reduceRight((sum, num) => sum.concat(num * 2), [] as number[]);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

Type assertions can hide type mismatches in the reducer callback. Use a type parameter on the call instead, so TypeScript checks the return type.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Use a type parameter instead of type assertion.

1 1 const arr: number[] = [1, 2, 3];
2 - arr.reduceRight((sum,·num)·=>·sum.concat(num·*·2),·[]·as·number[]);
2+ arr.reduceRight<number[]>((sum,·num)·=>·sum.concat(num·*·2),·[]);
3 3

const arr: number[] = [1, 2, 3];
arr.reduce<number[]>((sum, num) => sum.concat(num * 2), []);
arr.reduce((a, b) => a + b);
arr.reduce((sum, n) => sum + n, 0);