]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor: remove unnecessary type casts and improve type safety
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 1 Apr 2026 20:03:57 +0000 (22:03 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 1 Apr 2026 20:03:57 +0000 (22:03 +0200)
- 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

src/charging-station/ocpp/2.0/__testable__/index.ts
src/utils/MessageChannelUtils.ts
src/utils/Utils.ts
src/worker/WorkerFactory.ts

index 36a3d0487f5f689e2eca627caacdfb2db7a57984..bb328154e89e43a42c9e2f7b87c951b9b5946cc2 100644 (file)
@@ -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),
index 2ee6ccfec36558accb2b89afbbe477e60c0ba325..17c244a190c7859be769e607e3fb40b3051ceb64 100644 (file)
@@ -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,
index 9a9e55a6ce126a0b58c422590a8bbcf95499a03f..b7dbeef9ff85e1d9adf7ca291df5b509b2917d81 100644 (file)
@@ -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<string, unknown>).length === 0
-  }
-
-  if (valueType === 'Array') {
-    return (value as unknown[]).length === 0
-  }
-
-  if (valueType === 'Map') {
-    return (value as Map<unknown, unknown>).size === 0
-  }
-
-  if (valueType === 'Set') {
-    return (value as Set<unknown>).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 = <T extends object, S extends object>(target: T, source: S): T => {
   const output: Record<string, unknown> = { ...(target as Record<string, unknown>) }
index bfcf4152cde2c2378e5daa1b895ea9e745556948..f48b91eeaf53e5a1634387d78134b6f1ed721cd4 100644 (file)
@@ -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<D, R>(workerScript, workerOptions)
+        return new WorkerDynamicPool<D, R>(workerScript, resolvedOptions)
       case WorkerProcessType.fixedPool:
-        return new WorkerFixedPool<D, R>(workerScript, workerOptions)
+        return new WorkerFixedPool<D, R>(workerScript, resolvedOptions)
       case WorkerProcessType.workerSet:
-        return new WorkerSet<D, R>(workerScript, workerOptions)
+        return new WorkerSet<D, R>(workerScript, resolvedOptions)
       default:
         // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
         throw new Error(`Worker implementation type '${workerProcessType}' not found`)