useReduceTypeParameter
Ta treść nie jest jeszcze dostępna w Twoim języku.
Summary
Section titled “Summary”- Rule available since:
v2.4.12 - Diagnostic Category:
lint/nursery/useReduceTypeParameter - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
@typescript-eslint/prefer-reduce-type-parameter
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useReduceTypeParameter": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.