fix: resolve all TypeScript type errors across codebase for Deno strict mode

Comprehensive type safety improvements across all CLI handlers and daemon:

**Error handling type fixes:**
- Add 'error instanceof Error' checks before accessing error.message throughout
- Fix all error/retryError/stdError/upsError type assertions
- Replace direct error.message with proper type guards

**Switch case improvements:**
- Wrap case block declarations in braces to satisfy deno-lint
- Fix no-case-declarations warnings in CLI command handlers

**Null/undefined safety:**
- Add checks for config.snmp and config.thresholds before access
- Fix IUpsStatus lastStatusChange to handle undefined with default value
- Add proper null checks in legacy configuration paths

**Type annotations:**
- Add explicit type annotations to lambda parameters (groupId, updateAvailable, etc.)
- Add TUpsModel type cast for 'cyberpower' default
- Import and use INupstConfig type where needed

**Parameter type fixes:**
- Fix implicit 'any' type errors in array callbacks
- Add type annotations to filter/find/map parameters

Files modified:
- ts/cli.ts: config.snmp/thresholds null checks, unused error variable fixes
- ts/cli/group-handler.ts: 4 error.message fixes + 2 parameter type annotations
- ts/cli/service-handler.ts: 3 error.message fixes
- ts/cli/ups-handler.ts: 5 error.message fixes + config checks + TUpsModel import
- ts/daemon.ts: 8 error.message fixes + IUpsStatus lastStatusChange fix + updateAvailable type
- ts/nupst.ts: 1 error.message fix
- ts/systemd.ts: 5 error.message fixes + parameter type annotation

All tests passing (3/3 SNMP tests + 10/10 logger tests)
Type check: ✓ No errors
This commit is contained in:
2025-10-18 21:07:57 +00:00
parent 9ccbbbdc37
commit c2d39cc19a
7 changed files with 112 additions and 86 deletions

View File

@@ -179,11 +179,11 @@ export class NupstDaemon {
return this.config;
} catch (error) {
if (error.message && error.message.includes('No configuration found')) {
if (error instanceof Error && error.message && error.message.includes('No configuration found')) {
throw error; // Re-throw the no configuration error
}
this.logConfigError(`Error loading configuration: ${error.message}`);
this.logConfigError(`Error loading configuration: ${error instanceof Error ? error.message : String(error)}`);
throw new Error('Failed to load configuration');
}
}
@@ -252,7 +252,7 @@ export class NupstDaemon {
this.snmp.getNupst().logVersionInfo(false); // Don't check for updates immediately on startup
// Check for updates in the background
this.snmp.getNupst().checkForUpdates().then(updateAvailable => {
this.snmp.getNupst().checkForUpdates().then((updateAvailable: boolean) => {
if (updateAvailable) {
const updateStatus = this.snmp.getNupst().getUpdateStatus();
const boxWidth = 45;
@@ -272,7 +272,7 @@ export class NupstDaemon {
await this.monitor();
} catch (error) {
this.isRunning = false;
logger.error(`Daemon failed to start: ${error.message}`);
logger.error(`Daemon failed to start: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1); // Exit with error
}
}
@@ -373,7 +373,7 @@ export class NupstDaemon {
// Wait before next check
await this.sleep(this.config.checkInterval);
} catch (error) {
logger.error(`Error during UPS monitoring: ${error.message}`);
logger.error(`Error during UPS monitoring: ${error instanceof Error ? error.message : String(error)}`);
await this.sleep(this.config.checkInterval);
}
}
@@ -407,29 +407,31 @@ export class NupstDaemon {
// Get the current status from the map
const currentStatus = this.upsStatus.get(ups.id);
// Update status with new values
const updatedStatus = {
...currentStatus,
const updatedStatus: IUpsStatus = {
id: ups.id,
name: ups.name,
powerStatus: status.powerStatus,
batteryCapacity: status.batteryCapacity,
batteryRuntime: status.batteryRuntime,
lastCheckTime: currentTime
lastCheckTime: currentTime,
lastStatusChange: currentStatus?.lastStatusChange || currentTime
};
// Check if power status changed
if (currentStatus.powerStatus !== status.powerStatus) {
if (currentStatus && currentStatus.powerStatus !== status.powerStatus) {
logger.logBoxTitle(`Power Status Change: ${ups.name}`, 50);
logger.logBoxLine(`Status changed: ${currentStatus.powerStatus}${status.powerStatus}`);
logger.logBoxEnd();
updatedStatus.lastStatusChange = currentTime;
}
// Update the status in the map
this.upsStatus.set(ups.id, updatedStatus);
} catch (error) {
logger.error(`Error checking UPS ${ups.name} (${ups.id}): ${error.message}`);
logger.error(`Error checking UPS ${ups.name} (${ups.id}): ${error instanceof Error ? error.message : String(error)}`);
}
}
}
@@ -631,7 +633,7 @@ export class NupstDaemon {
});
logger.log(`Shutdown initiated: ${stdout}`);
} catch (e) {
throw new Error(`Shutdown command not found: ${e.message}`);
throw new Error(`Shutdown command not found: ${e instanceof Error ? e.message : String(e)}`);
}
}
@@ -725,14 +727,14 @@ export class NupstDaemon {
return;
}
} catch (upsError) {
logger.error(`Error checking UPS ${ups.name} during shutdown: ${upsError.message}`);
logger.error(`Error checking UPS ${ups.name} during shutdown: ${upsError instanceof Error ? upsError.message : String(upsError)}`);
}
}
// Wait before checking again
await this.sleep(CHECK_INTERVAL);
} catch (error) {
logger.error(`Error monitoring UPS during shutdown: ${error.message}`);
logger.error(`Error monitoring UPS during shutdown: ${error instanceof Error ? error.message : String(error)}`);
await this.sleep(CHECK_INTERVAL);
}
}