From: Jérôme Benoit Date: Wed, 1 Apr 2026 20:03:57 +0000 (+0200) Subject: refactor: remove unnecessary type casts and improve type safety X-Git-Tag: ocpp-server@v4.2.0~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ccb61c1cb0155ee414c99ce2648c7dcf661f605e;p=e-mobility-charging-stations-simulator.git refactor: remove unnecessary type casts and improve type safety - Remove redundant as any cast in __testable__/index.ts - Rewrite isEmpty() with native type guards (typeof/instanceof) - Replace parameter mutation with const in WorkerFactory - Remove unnecessary cast in MessageChannelUtils --- diff --git a/src/charging-station/ocpp/2.0/__testable__/index.ts b/src/charging-station/ocpp/2.0/__testable__/index.ts index 36a3d048..bb328154 100644 --- a/src/charging-station/ocpp/2.0/__testable__/index.ts +++ b/src/charging-station/ocpp/2.0/__testable__/index.ts @@ -271,8 +271,7 @@ export function createTestableIncomingRequestService ( const serviceImpl = service as unknown as TestableOCPP20IncomingRequestService return { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment - buildReportData: (serviceImpl as any).buildReportData.bind(service), + buildReportData: serviceImpl.buildReportData.bind(service), handleRequestCertificateSigned: serviceImpl.handleRequestCertificateSigned.bind(service), handleRequestChangeAvailability: serviceImpl.handleRequestChangeAvailability.bind(service), handleRequestClearCache: serviceImpl.handleRequestClearCache.bind(service), diff --git a/src/utils/MessageChannelUtils.ts b/src/utils/MessageChannelUtils.ts index 2ee6ccfe..17c244a1 100644 --- a/src/utils/MessageChannelUtils.ts +++ b/src/utils/MessageChannelUtils.ts @@ -5,7 +5,6 @@ import type { ChargingStation } from '../charging-station/index.js' import { type ChargingStationData, type ChargingStationInfo, - type ChargingStationOcppConfiguration, type ChargingStationWorkerMessage, ChargingStationWorkerMessageEvents, type Statistics, @@ -101,8 +100,7 @@ const buildChargingStationDataPayload = (chargingStation: ChargingStation): Char bootNotificationResponse: chargingStation.bootNotificationResponse, connectors: buildConnectorEntries(chargingStation), evses: buildEvseEntries(chargingStation), - ocppConfiguration: - chargingStation.ocppConfiguration ?? ({} as ChargingStationOcppConfiguration), + ocppConfiguration: chargingStation.ocppConfiguration ?? {}, started: chargingStation.started, stationInfo: chargingStation.stationInfo ?? ({} as ChargingStationInfo), supervisionUrl: chargingStation.wsConnectionUrl.href, diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 9a9e55a6..b7dbeef9 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -66,40 +66,28 @@ const type = (value: unknown): string => { return Object.prototype.toString.call(value).slice(8, -1) } +const isObject = (value: unknown): value is object => { + return type(value) === 'Object' +} + export const isEmpty = (value: unknown): boolean => { - const valueType = type(value) - if (['BigInt', 'Boolean', 'NaN', 'Null', 'Number', 'Undefined'].includes(valueType)) { + if ( + value == null || + typeof value === 'bigint' || + typeof value === 'boolean' || + typeof value === 'number' + ) { return false } - if (!value) return true - - if (valueType === 'String') { - return (value as string).trim().length === 0 - } - - if (valueType === 'Object') { - return Object.keys(value as Record).length === 0 - } - - if (valueType === 'Array') { - return (value as unknown[]).length === 0 - } - - if (valueType === 'Map') { - return (value as Map).size === 0 - } - - if (valueType === 'Set') { - return (value as Set).size === 0 - } + if (typeof value === 'string') return value.trim().length === 0 + if (Array.isArray(value)) return value.length === 0 + if (value instanceof Map) return value.size === 0 + if (value instanceof Set) return value.size === 0 + if (isObject(value)) return Object.keys(value).length === 0 return false } -const isObject = (value: unknown): value is object => { - return type(value) === 'Object' -} - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters export const mergeDeepRight = (target: T, source: S): T => { const output: Record = { ...(target as Record) } diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index bfcf4152..f48b91ee 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -23,14 +23,14 @@ export class WorkerFactory { if (!isMainThread) { throw new Error('Cannot get a worker implementation outside the main thread') } - workerOptions = mergeDeepRight(DEFAULT_WORKER_OPTIONS, (workerOptions ?? {}) as WorkerOptions) + const resolvedOptions = mergeDeepRight(DEFAULT_WORKER_OPTIONS, workerOptions ?? {}) switch (workerProcessType) { case WorkerProcessType.dynamicPool: - return new WorkerDynamicPool(workerScript, workerOptions) + return new WorkerDynamicPool(workerScript, resolvedOptions) case WorkerProcessType.fixedPool: - return new WorkerFixedPool(workerScript, workerOptions) + return new WorkerFixedPool(workerScript, resolvedOptions) case WorkerProcessType.workerSet: - return new WorkerSet(workerScript, workerOptions) + return new WorkerSet(workerScript, resolvedOptions) default: // eslint-disable-next-line @typescript-eslint/restrict-template-expressions throw new Error(`Worker implementation type '${workerProcessType}' not found`)