Skip to content

Commit 1432791

Browse files
committed
fix(types): allow job static dispatch methods on DI-backed subclasses
1 parent 2f279a4 commit 1432791

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/job.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export abstract class Job<Payload = any> {
171171
* ```
172172
*/
173173
static dispatch<T extends Job>(
174-
this: new (...args: unknown[]) => T,
174+
this: abstract new (...args: any[]) => T,
175175
payload: T extends Job<infer P> ? P : never
176176
): JobDispatcher<T extends Job<infer P> ? P : never> {
177177
const jobClass = this as unknown as { options?: JobOptions; name: string }
@@ -223,7 +223,7 @@ export abstract class Job<Payload = any> {
223223
* ```
224224
*/
225225
static dispatchMany<T extends Job>(
226-
this: new (...args: unknown[]) => T,
226+
this: abstract new (...args: any[]) => T,
227227
payloads: (T extends Job<infer P> ? P : never)[]
228228
): JobBatchDispatcher<T extends Job<infer P> ? P : never> {
229229
const jobClass = this as unknown as { options?: JobOptions; name: string }
@@ -273,7 +273,7 @@ export abstract class Job<Payload = any> {
273273
* ```
274274
*/
275275
static schedule<T extends Job>(
276-
this: new (...args: unknown[]) => T,
276+
this: abstract new (...args: any[]) => T,
277277
payload: T extends Job<infer P> ? P : never
278278
): ScheduleBuilder<T extends Job<infer P> ? P : never> {
279279
const jobClass = this as unknown as { options?: JobOptions; name: string }

tests/job.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { test } from '@japa/runner'
2+
import { Job } from '../src/job.js'
3+
import { JobDispatcher } from '../src/job_dispatcher.js'
4+
import { JobBatchDispatcher } from '../src/job_batch_dispatcher.js'
5+
import { ScheduleBuilder } from '../src/schedule_builder.js'
6+
7+
class PaymentService {}
8+
9+
class ProcessPaymentJob extends Job<{ paymentId: string }> {
10+
constructor(protected paymentService: PaymentService) {
11+
super()
12+
}
13+
14+
async execute() {
15+
void this.paymentService
16+
}
17+
}
18+
19+
test.group('Job static methods', () => {
20+
test('should support subclasses with typed constructor injection', ({ assert, expectTypeOf }) => {
21+
const dispatcher = ProcessPaymentJob.dispatch({ paymentId: 'pay_123' })
22+
const batchDispatcher = ProcessPaymentJob.dispatchMany([{ paymentId: 'pay_123' }])
23+
const schedule = ProcessPaymentJob.schedule({ paymentId: 'pay_123' })
24+
25+
assert.instanceOf(dispatcher, JobDispatcher)
26+
assert.instanceOf(batchDispatcher, JobBatchDispatcher)
27+
assert.instanceOf(schedule, ScheduleBuilder)
28+
29+
expectTypeOf(dispatcher).toEqualTypeOf<JobDispatcher<{ paymentId: string }>>()
30+
expectTypeOf(batchDispatcher).toEqualTypeOf<JobBatchDispatcher<{ paymentId: string }>>()
31+
expectTypeOf(schedule).toEqualTypeOf<ScheduleBuilder<{ paymentId: string }>>()
32+
})
33+
})

0 commit comments

Comments
 (0)