From 74a33a25bb1b65d9032dafc6bf0f9516e7a40aa6 Mon Sep 17 00:00:00 2001 From: Hiromasa Ihara Date: Sat, 6 Jul 2024 19:13:08 +0900 Subject: [PATCH] fix: decrease using type of any to get typing benefit --- .../view/component/changelog.constants.ts | 6 ++--- ui/src/app/edge/history/historydataservice.ts | 2 +- .../Ess/TimeOfUseTariff/flat/flat.ts | 2 +- .../Ess/TimeOfUseTariff/modal/modal.ts | 2 +- .../modal/evcs-chart/evcs.chart.ts | 2 +- .../section/abstractsection.component.ts | 12 +++++----- .../components/pickdate/pickdate.component.ts | 6 ++--- ui/src/app/shared/service/arrayutils.ts | 2 +- ui/src/app/shared/service/myerrorhandler.ts | 2 ++ ui/src/app/shared/service/service.ts | 2 ++ .../app/shared/service/test/dummyservice.ts | 2 ++ ui/src/app/shared/service/utils.ts | 22 +++++++++---------- ui/src/app/shared/type/language.ts | 10 +++++++-- ui/src/app/shared/utils/array/array.utils.ts | 2 +- ui/src/zone-flags.ts | 2 ++ 15 files changed, 45 insertions(+), 31 deletions(-) diff --git a/ui/src/app/changelog/view/component/changelog.constants.ts b/ui/src/app/changelog/view/component/changelog.constants.ts index 473e291c675..21a9e907c85 100644 --- a/ui/src/app/changelog/view/component/changelog.constants.ts +++ b/ui/src/app/changelog/view/component/changelog.constants.ts @@ -37,13 +37,13 @@ export class Product { public static readonly OPENEMS_BACKEND = new Product('OpenEMS Edge', 'https://github.com/OpenEMS/openems'); // private to disallow creating other instances of this type - private constructor(public readonly name: string, public readonly url: any) { + private constructor(public readonly name: string, public readonly url: string) { } } export class App { // private to disallow creating other instances of this type - private constructor(public readonly name: string, public readonly url: any) { + private constructor(public readonly name: string, public readonly url: string) { } } @@ -52,7 +52,7 @@ export class OpenemsComponent { public static readonly SDM630_ZAEHLER = new OpenemsComponent('SDM 630 Zähler', 'https://github.com/OpenEMS/openems/tree/develop/io.openems.edge.meter.microcare.sdm630'); // private to disallow creating other instances of this type - private constructor(public readonly name: string, public readonly url: any) { + private constructor(public readonly name: string, public readonly url: string) { } } diff --git a/ui/src/app/edge/history/historydataservice.ts b/ui/src/app/edge/history/historydataservice.ts index 29987b5df8f..bf64a942674 100644 --- a/ui/src/app/edge/history/historydataservice.ts +++ b/ui/src/app/edge/history/historydataservice.ts @@ -13,7 +13,7 @@ import { RefresherCustomEvent } from "@ionic/angular"; @Injectable() export class HistoryDataService extends DataService { - public queryChannelsTimeout: any | null = null; + public queryChannelsTimeout: ReturnType | null = null; protected override timestamps: string[] = []; private channelAddresses: { [sourceId: string]: ChannelAddress } = {}; diff --git a/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/flat/flat.ts b/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/flat/flat.ts index 5aa766847dd..72cccc4ce15 100644 --- a/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/flat/flat.ts +++ b/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/flat/flat.ts @@ -12,7 +12,7 @@ export class FlatComponent extends AbstractFlatWidget implements OnInit { protected readonly CONVERT_MODE_TO_MANUAL_OFF_AUTOMATIC = Utils.CONVERT_MODE_TO_MANUAL_OFF_AUTOMATIC(this.translate); protected readonly CONVERT_TIME_OF_USE_TARIFF_STATE = Utils.CONVERT_TIME_OF_USE_TARIFF_STATE(this.translate); - protected priceWithCurrency: any; + protected priceWithCurrency: string; async presentModal() { const modal = await this.modalController.create({ diff --git a/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/modal/modal.ts b/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/modal/modal.ts index f124fd866b1..b4a182fd769 100644 --- a/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/modal/modal.ts +++ b/ui/src/app/edge/live/Controller/Ess/TimeOfUseTariff/modal/modal.ts @@ -11,7 +11,7 @@ import { Controller_Ess_TimeOfUseTariff } from '../Ess_TimeOfUseTariff'; export class ModalComponent extends AbstractModal { protected readonly CONVERT_TIME_OF_USE_TARIFF_STATE = this.Utils.CONVERT_TIME_OF_USE_TARIFF_STATE(this.translate); - protected priceWithCurrency: any; + protected priceWithCurrency: string; protected override getFormGroup(): FormGroup { return this.formBuilder.group({ diff --git a/ui/src/app/edge/live/Multiple/Evcs_Api_Cluster/modal/evcs-chart/evcs.chart.ts b/ui/src/app/edge/live/Multiple/Evcs_Api_Cluster/modal/evcs-chart/evcs.chart.ts index fda2c353d70..fec7d8e1ebb 100644 --- a/ui/src/app/edge/live/Multiple/Evcs_Api_Cluster/modal/evcs-chart/evcs.chart.ts +++ b/ui/src/app/edge/live/Multiple/Evcs_Api_Cluster/modal/evcs-chart/evcs.chart.ts @@ -23,7 +23,7 @@ export class EvcsChartComponent implements OnInit, OnChanges { public loading: boolean = true; public options: BarChartOptions; - public labels: any[]; + public labels: string[]; public datasets: Chart.ChartDataset[]; public chart: Chart.Chart; // This will hold our chart info diff --git a/ui/src/app/edge/live/energymonitor/chart/section/abstractsection.component.ts b/ui/src/app/edge/live/energymonitor/chart/section/abstractsection.component.ts index f8174407548..9a6dfc9b214 100644 --- a/ui/src/app/edge/live/energymonitor/chart/section/abstractsection.component.ts +++ b/ui/src/app/edge/live/energymonitor/chart/section/abstractsection.component.ts @@ -278,12 +278,12 @@ export abstract class AbstractSection { } /** - * calculate... - * ...length of square and image; - * ...x and y of text and image; - * ...fontsize of text; - */ - private getSquare(innerRadius: any): SvgSquare { + * calculate... + * ...length of square and image; + * ...x and y of text and image; + * ...fontsize of text; + */ + private getSquare(innerRadius: number): SvgSquare { const width = innerRadius / 2.5; const textSize = width / 4; diff --git a/ui/src/app/shared/components/pickdate/pickdate.component.ts b/ui/src/app/shared/components/pickdate/pickdate.component.ts index 03295585058..885544f107a 100644 --- a/ui/src/app/shared/components/pickdate/pickdate.component.ts +++ b/ui/src/app/shared/components/pickdate/pickdate.component.ts @@ -176,7 +176,7 @@ export class PickDateComponent implements OnInit, OnDestroy { } case DefaultTypes.PeriodString.CUSTOM: { - let dateDistance = Math.floor(Math.abs(this.service.historyPeriod.value.from - this.service.historyPeriod.value.to) / (1000 * 60 * 60 * 24)); + let dateDistance = Math.floor(Math.abs(this.service.historyPeriod.value.from.getTime() - this.service.historyPeriod.value.to.getTime()) / (1000 * 60 * 60 * 24)); if (dateDistance == 0) { dateDistance = 1; } @@ -271,7 +271,7 @@ export class PickDateComponent implements OnInit, OnDestroy { break; } case DefaultTypes.PeriodString.CUSTOM: { - let dateDistance = Math.floor(Math.abs(this.service.historyPeriod.value.from - this.service.historyPeriod.value.to) / (1000 * 60 * 60 * 24)); + let dateDistance = Math.floor(Math.abs(this.service.historyPeriod.value.from.getTime() - this.service.historyPeriod.value.to.getTime()) / (1000 * 60 * 60 * 24)); if (dateDistance == 0) { dateDistance = 1; } @@ -322,7 +322,7 @@ export class PickDateComponent implements OnInit, OnDestroy { break; } case DefaultTypes.PeriodString.CUSTOM: { - let dateDistance = Math.floor(Math.abs(this.service.historyPeriod.value.from - this.service.historyPeriod.value.to) / (1000 * 60 * 60 * 24)); + let dateDistance = Math.floor(Math.abs(this.service.historyPeriod.value.from.getTime() - this.service.historyPeriod.value.to.getTime()) / (1000 * 60 * 60 * 24)); if (dateDistance == 0) { dateDistance = 1; } diff --git a/ui/src/app/shared/service/arrayutils.ts b/ui/src/app/shared/service/arrayutils.ts index 9735b3dfe6a..f2bb14e9df2 100644 --- a/ui/src/app/shared/service/arrayutils.ts +++ b/ui/src/app/shared/service/arrayutils.ts @@ -1,5 +1,5 @@ export namespace ArrayUtils { - export function equalsCheck(a: any[], b: any[]) { + export function equalsCheck(a: T[], b: T[]) { return a.length === b.length && a.every((v, i) => v === b[i]); } diff --git a/ui/src/app/shared/service/myerrorhandler.ts b/ui/src/app/shared/service/myerrorhandler.ts index a4ebbfee278..faf283b744e 100644 --- a/ui/src/app/shared/service/myerrorhandler.ts +++ b/ui/src/app/shared/service/myerrorhandler.ts @@ -8,6 +8,8 @@ export class MyErrorHandler implements ErrorHandler { private injector: Injector, ) { } + // https://v16.angular.io/api/core/ErrorHandler#errorhandler + // eslint-disable-next-line @typescript-eslint/no-explicit-any handleError(error: any) { const logger = this.injector.get(Logger); console.error(error); diff --git a/ui/src/app/shared/service/service.ts b/ui/src/app/shared/service/service.ts index f8ab06785bc..d8c19586358 100644 --- a/ui/src/app/shared/service/service.ts +++ b/ui/src/app/shared/service/service.ts @@ -132,6 +132,8 @@ export class Service extends AbstractService { this.notificationEvent.next(notification); } + // https://v16.angular.io/api/core/ErrorHandler#errorhandler + // eslint-disable-next-line @typescript-eslint/no-explicit-any public override handleError(error: any) { console.error(error); // TODO: show notification diff --git a/ui/src/app/shared/service/test/dummyservice.ts b/ui/src/app/shared/service/test/dummyservice.ts index 2d654a59e23..e8b9cdc102f 100644 --- a/ui/src/app/shared/service/test/dummyservice.ts +++ b/ui/src/app/shared/service/test/dummyservice.ts @@ -62,6 +62,8 @@ export class DummyService extends AbstractService { isPartnerAllowed(edge: Edge): boolean { throw new Error("Method not implemented."); } + // https://v16.angular.io/api/core/ErrorHandler#errorhandler + // eslint-disable-next-line @typescript-eslint/no-explicit-any override handleError(error: any): void { throw new Error("Method not implemented."); } diff --git a/ui/src/app/shared/service/utils.ts b/ui/src/app/shared/service/utils.ts index 1d9c6873350..67a0376141f 100644 --- a/ui/src/app/shared/service/utils.ts +++ b/ui/src/app/shared/service/utils.ts @@ -19,14 +19,14 @@ export class Utils { * @param element * @param array */ - public static isLastElement(element, array: any[]) { + public static isLastElement(element: T, array: T[]): boolean { return element == array[array.length - 1]; } /** * Creates a deep copy of the object */ - public static deepCopy(obj: any, target?: any) { + public static deepCopy(obj: T, target?: T): T { let copy: any; // Handle the 3 simple types, and null or undefined @@ -162,7 +162,7 @@ export class Utils { * @param v2 * @returns */ - public static compareArraysSafely(v1: any[], v2: any[]): boolean { + public static compareArraysSafely(v1: T[] | null, v2: T[] | null): boolean { if (v1 == null || v2 == null) { return null; } @@ -236,7 +236,7 @@ export class Utils { * @param value the value from passed value in html * @returns converted value */ - public static CONVERT_TO_WATT = (value: any): string => { + public static CONVERT_TO_WATT = (value: number | null): string => { if (value == null) { return '-'; } else if (value >= 0) { @@ -252,7 +252,7 @@ export class Utils { * @param value the value from passed value in html * @returns converted value */ - public static CONVERT_WATT_TO_KILOWATT = (value: any): string => { + public static CONVERT_WATT_TO_KILOWATT = (value: number | null): string => { if (value == null) { return '-'; } @@ -271,7 +271,7 @@ export class Utils { * @param value the value from passed value in html * @returns converted value */ - public static CONVERT_SECONDS_TO_DATE_FORMAT = (value: any): string => { + public static CONVERT_SECONDS_TO_DATE_FORMAT = (value: number): string => { return new Date(value * 1000).toLocaleTimeString(); }; @@ -291,7 +291,7 @@ export class Utils { * @param value the value from passed value in html * @returns converted value */ - public static CONVERT_TO_WATTHOURS = (value: any): string => { + public static CONVERT_TO_WATTHOURS = (value: number): string => { return formatNumber(value, 'de', '1.0-1') + ' Wh'; }; @@ -301,7 +301,7 @@ export class Utils { * @param value the value from passed value in html * @returns converted value */ - public static CONVERT_TO_KILO_WATTHOURS = (value: any): string => { + public static CONVERT_TO_KILO_WATTHOURS = (value: number): string => { return formatNumber(Utils.divideSafely(value, 1000), 'de', '1.0-1') + ' kWh'; }; @@ -381,7 +381,7 @@ export class Utils { * @returns converted value */ public static CONVERT_PRICE_TO_CENT_PER_KWH = (decimal: number, label: string) => { - return (value: any): string => + return (value: number | null): string => (!value ? "-" : formatNumber(value / 10, 'de', '1.0-' + decimal)) + ' ' + label; }; @@ -517,7 +517,7 @@ export class Utils { * @param array the array to be shuffled * @returns the shuffled array */ - public static shuffleArray(array: any[]): any[] { + public static shuffleArray(array: T[]): T[] { return array.sort(() => Math.random() - 0.5); } @@ -529,7 +529,7 @@ export class Utils { * @param source the source Object. * @returns the value. */ - public static isArrayExistingInSource(arrayToCheck: string[], source: any): boolean { + public static isArrayExistingInSource(arrayToCheck: string[], source: Record): boolean { return arrayToCheck.every(value => { if (value in source) { return true; diff --git a/ui/src/app/shared/type/language.ts b/ui/src/app/shared/type/language.ts index 4655f872cf2..4737bf4510a 100644 --- a/ui/src/app/shared/type/language.ts +++ b/ui/src/app/shared/type/language.ts @@ -14,9 +14,13 @@ import fr from 'src/assets/i18n/fr.json'; import nl from 'src/assets/i18n/nl.json'; import ja from 'src/assets/i18n/ja.json'; +interface Translation { + [key: string]: string | Translation; +} + export class MyTranslateLoader implements TranslateLoader { - public getTranslation(key: string): Observable { + public getTranslation(key: string): Observable { const language = Language.getByKey(key); if (language) { return of(language.json); @@ -43,6 +47,9 @@ export class Language { public readonly key: string, public readonly i18nLocaleKey: string, public readonly json: any, + // Angular is not providing common type for locale. + // https://github.com/angular/angular/issues/30506 + // eslint-disable-next-line @typescript-eslint/no-explicit-any public readonly locale: any, ) { } @@ -100,5 +107,4 @@ export class Language { return lang?.i18nLocaleKey ?? Language.DEFAULT.i18nLocaleKey; } - } diff --git a/ui/src/app/shared/utils/array/array.utils.ts b/ui/src/app/shared/utils/array/array.utils.ts index f9e50f938b4..c163fe21540 100644 --- a/ui/src/app/shared/utils/array/array.utils.ts +++ b/ui/src/app/shared/utils/array/array.utils.ts @@ -1,5 +1,5 @@ export namespace ArrayUtils { - export function equalsCheck(a: any[], b: any[]) { + export function equalsCheck(a: T[], b: T[]): boolean { return a.length === b.length && a.every((v, i) => v === b[i]); } diff --git a/ui/src/zone-flags.ts b/ui/src/zone-flags.ts index c84245fd390..42fd6600d93 100644 --- a/ui/src/zone-flags.ts +++ b/ui/src/zone-flags.ts @@ -1,6 +1,8 @@ /** * Prevents Angular change detection from * running with certain Web Component callbacks + * https://ionicframework.com/docs/ja/troubleshooting/runtime#angular-change-detection */ // eslint-disable-next-line no-underscore-dangle +// eslint-disable-next-line @typescript-eslint/no-explicit-any (window as any).__Zone_disable_customElements = true;