From ee9d470a3b6e9c47f5c33a62a72ae2c6fa7ee98e Mon Sep 17 00:00:00 2001 From: Maksim Ivanov Date: Thu, 30 Nov 2023 12:45:41 +0300 Subject: [PATCH] fix(kit): possible type error reduce of empty array with no initial value (#6107) --- .../components/bar-chart/bar-chart.component.ts | 1 + .../components/input-time/input-time.component.ts | 12 +++++++----- .../input-time/test/input-time.component.spec.ts | 2 +- .../native-multi-select-group.component.ts | 2 +- .../native-select/native-select-group.component.ts | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/projects/addon-charts/components/bar-chart/bar-chart.component.ts b/projects/addon-charts/components/bar-chart/bar-chart.component.ts index 8aca27553231..1c5a85c89c83 100644 --- a/projects/addon-charts/components/bar-chart/bar-chart.component.ts +++ b/projects/addon-charts/components/bar-chart/bar-chart.component.ts @@ -94,6 +94,7 @@ export class TuiBarChartComponent { private getMax(values: ReadonlyArray, collapsed: boolean): number { return collapsed ? Math.max( + // eslint-disable-next-line no-restricted-syntax ...values.reduce((result, next) => result.map((value, index) => value + next[index]), ), diff --git a/projects/kit/components/input-time/input-time.component.ts b/projects/kit/components/input-time/input-time.component.ts index 70753f24380a..b8498d195998 100644 --- a/projects/kit/components/input-time/input-time.component.ts +++ b/projects/kit/components/input-time/input-time.component.ts @@ -296,11 +296,13 @@ export class TuiInputTimeComponent } private findNearestTimeFromItems(value: TuiTime): TuiTime | null { - return this.items.reduce((previous, current) => - Math.abs(current.toAbsoluteMilliseconds() - value.toAbsoluteMilliseconds()) < - Math.abs(previous.toAbsoluteMilliseconds() - value.toAbsoluteMilliseconds()) - ? current - : previous, + return this.items.reduce( + (previous, current) => + Math.abs(current.valueOf() - value.valueOf()) < + Math.abs(previous.valueOf() - value.valueOf()) + ? current + : previous, + new TuiTime(0, 0), ); } diff --git a/projects/kit/components/input-time/test/input-time.component.spec.ts b/projects/kit/components/input-time/test/input-time.component.spec.ts index 634db54819ce..4f1fa69d3667 100644 --- a/projects/kit/components/input-time/test/input-time.component.spec.ts +++ b/projects/kit/components/input-time/test/input-time.component.spec.ts @@ -53,7 +53,7 @@ describe(`InputTime`, () => { control = new FormControl(new TuiTime(12, 30)); cleaner = false; readOnly = false; - items: TuiTime[] | null = []; + items: TuiTime[] = []; labelOutside = false; size: TuiSizeL | TuiSizeS = `l`; strict = false; diff --git a/projects/kit/components/multi-select/native-multi-select/native-multi-select-group.component.ts b/projects/kit/components/multi-select/native-multi-select/native-multi-select-group.component.ts index 4cfdbda24771..27ee4e9cc03f 100644 --- a/projects/kit/components/multi-select/native-multi-select/native-multi-select-group.component.ts +++ b/projects/kit/components/multi-select/native-multi-select/native-multi-select-group.component.ts @@ -41,7 +41,7 @@ export class TuiNativeMultiSelectGroupComponent< onValueChange(selectedOptions: HTMLSelectElement['selectedOptions']): void { const selected = Array.from(selectedOptions).map(option => option.index); - const flatItems = this.items?.reduce((acc, val) => acc.concat(val)) || []; + const flatItems = this.items?.reduce((acc, val) => acc.concat(val), []) || []; const value = flatItems.filter((_, index) => selected.includes(index)); this.host.onSelectionChange(value); diff --git a/projects/kit/components/select/native-select/native-select-group.component.ts b/projects/kit/components/select/native-select/native-select-group.component.ts index 9e68f32c2f73..4213103be5f5 100644 --- a/projects/kit/components/select/native-select/native-select-group.component.ts +++ b/projects/kit/components/select/native-select/native-select-group.component.ts @@ -49,7 +49,7 @@ export class TuiNativeSelectGroupComponent extends AbstractTuiNativeSelect< } onValueChange(index: number): void { - const flatItems = this.items?.reduce((acc, val) => acc.concat(val)); + const flatItems = this.items?.reduce((acc, val) => acc.concat(val), []); this.host.onValueChange(flatItems?.[index] || null); }