From bf66bfbbcb1afd7079ae8964aa91e84096ed1e61 Mon Sep 17 00:00:00 2001 From: Parth Arsid Date: Tue, 31 Mar 2026 23:07:35 +0530 Subject: [PATCH] refactor: simplify exclusive timeZone/utcOffset parameter handling Remove redundant constructor overloads from CronJob and CronTime. The CronJobParams type already enforces mutual exclusivity of timeZone and utcOffset via a discriminated union with 'never' types. Changes: - CronJob: Replace 3 constructor overloads with a single signature - CronJob: Simplify CronTime instantiation to a single call - CronJob.from(): Remove duplicated runtime check and branching, delegate to constructor which already validates - CronTime: Replace 3 constructor overloads with a single signature The runtime check for JS users is preserved in both constructors. Fixes #704 --- src/job.ts | 114 +++++++++------------------------------------------- src/time.ts | 14 +------ 2 files changed, 22 insertions(+), 106 deletions(-) diff --git a/src/job.ts b/src/job.ts index 4e301194..a94d94f2 100644 --- a/src/job.ts +++ b/src/job.ts @@ -43,40 +43,10 @@ export class CronJob { onTick: CronJobParams['onTick'], onComplete?: CronJobParams['onComplete'], start?: CronJobParams['start'], - timeZone?: CronJobParams['timeZone'], + timeZone?: string | null, context?: CronJobParams['context'], runOnInit?: CronJobParams['runOnInit'], - utcOffset?: null, - unrefTimeout?: CronJobParams['unrefTimeout'], - waitForCompletion?: CronJobParams['waitForCompletion'], - errorHandler?: CronJobParams['errorHandler'], - name?: CronJobParams['name'], - threshold?: CronJobParams['threshold'] - ); - constructor( - cronTime: CronJobParams['cronTime'], - onTick: CronJobParams['onTick'], - onComplete?: CronJobParams['onComplete'], - start?: CronJobParams['start'], - timeZone?: null, - context?: CronJobParams['context'], - runOnInit?: CronJobParams['runOnInit'], - utcOffset?: CronJobParams['utcOffset'], - unrefTimeout?: CronJobParams['unrefTimeout'], - waitForCompletion?: CronJobParams['waitForCompletion'], - errorHandler?: CronJobParams['errorHandler'], - name?: CronJobParams['name'], - threshold?: CronJobParams['threshold'] - ); - constructor( - cronTime: CronJobParams['cronTime'], - onTick: CronJobParams['onTick'], - onComplete?: CronJobParams['onComplete'], - start?: CronJobParams['start'], - timeZone?: CronJobParams['timeZone'], - context?: CronJobParams['context'], - runOnInit?: CronJobParams['runOnInit'], - utcOffset?: CronJobParams['utcOffset'], + utcOffset?: number | null, unrefTimeout?: CronJobParams['unrefTimeout'], waitForCompletion?: CronJobParams['waitForCompletion'], errorHandler?: CronJobParams['errorHandler'], @@ -93,13 +63,7 @@ export class CronJob { throw new ExclusiveParametersError('timeZone', 'utcOffset'); } - if (timeZone != null) { - this.cronTime = new CronTime(cronTime, timeZone, null); - } else if (utcOffset != null) { - this.cronTime = new CronTime(cronTime, null, utcOffset); - } else { - this.cronTime = new CronTime(cronTime, timeZone, utcOffset); - } + this.cronTime = new CronTime(cronTime, timeZone ?? null, utcOffset ?? null); if (unrefTimeout != null) { this.unrefTimeout = unrefTimeout; @@ -137,61 +101,23 @@ export class CronJob { static from( params: CronJobParams ) { - // runtime check for JS users - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - if (params.timeZone != null && params.utcOffset != null) { - throw new ExclusiveParametersError('timeZone', 'utcOffset'); - } - - if (params.timeZone != null) { - return new CronJob( - params.cronTime, - params.onTick, - params.onComplete, - params.start, - params.timeZone, - params.context, - params.runOnInit, - params.utcOffset, - params.unrefTimeout, - params.waitForCompletion, - params.errorHandler, - params.name, - params.threshold - ); - } else if (params.utcOffset != null) { - return new CronJob( - params.cronTime, - params.onTick, - params.onComplete, - params.start, - null, - params.context, - params.runOnInit, - params.utcOffset, - params.unrefTimeout, - params.waitForCompletion, - params.errorHandler, - params.name, - params.threshold - ); - } else { - return new CronJob( - params.cronTime, - params.onTick, - params.onComplete, - params.start, - params.timeZone, - params.context, - params.runOnInit, - params.utcOffset, - params.unrefTimeout, - params.waitForCompletion, - params.errorHandler, - params.name, - params.threshold - ); - } + // The constructor's runtime check handles the exclusivity validation for JS users. + // The CronJobParams type enforces it at compile time for TS users. + return new CronJob( + params.cronTime, + params.onTick, + params.onComplete, + params.start, + ('timeZone' in params ? params.timeZone : null) as string | null, + params.context, + params.runOnInit, + ('utcOffset' in params ? params.utcOffset : null) as number | null, + params.unrefTimeout, + params.waitForCompletion, + params.errorHandler, + params.name, + params.threshold + ); } private _fnWrap(cmd: CronCommand): CronCallback { diff --git a/src/time.ts b/src/time.ts index 137ea42e..6de2aba0 100644 --- a/src/time.ts +++ b/src/time.ts @@ -43,18 +43,8 @@ export class CronTime { constructor( source: CronJobParams['cronTime'], - timeZone?: CronJobParams['timeZone'], - utcOffset?: null - ); - constructor( - source: CronJobParams['cronTime'], - timeZone?: null, - utcOffset?: CronJobParams['utcOffset'] - ); - constructor( - source: CronJobParams['cronTime'], - timeZone?: CronJobParams['timeZone'], - utcOffset?: CronJobParams['utcOffset'] + timeZone?: string | null, + utcOffset?: number | null ) { // runtime check for JS users if (timeZone != null && utcOffset != null) {