diff --git a/packages/test/src/test-mockactivityenv.ts b/packages/test/src/test-mockactivityenv.ts index 6811cf06b..cfdc2eb35 100644 --- a/packages/test/src/test-mockactivityenv.ts +++ b/packages/test/src/test-mockactivityenv.ts @@ -47,3 +47,13 @@ test('MockActivityEnvironment injects provided info', async (t) => { }, 1); t.is(res, 4); }); + +test('MockActivityEnvironment return type is correctly inferred', async (t) => { + async function foo(): Promise { + return 'foo'; + } + const result = await new MockActivityEnvironment().run(foo); + // There should be no compile time error on this line + result.startsWith('foo'); + t.pass(); +}); diff --git a/packages/testing/src/mocking-activity-environment.ts b/packages/testing/src/mocking-activity-environment.ts index b9f9c05b0..b6a87cfcd 100644 --- a/packages/testing/src/mocking-activity-environment.ts +++ b/packages/testing/src/mocking-activity-environment.ts @@ -58,8 +58,8 @@ export class MockActivityEnvironment extends events.EventEmitter { /** * Run a function in Activity Context */ - public async run

>(fn: F, ...args: P): Promise { - return this.activity.runNoEncoding(fn as ActivityFunction, { args, headers: {} }) as Promise; + public run(fn: F, ...args: Parameters): ReturnType { + return this.activity.runNoEncoding(fn, { args, headers: {} }) as ReturnType; } } diff --git a/packages/worker/src/activity.ts b/packages/worker/src/activity.ts index 7614017d0..0c2103d37 100644 --- a/packages/worker/src/activity.ts +++ b/packages/worker/src/activity.ts @@ -193,9 +193,9 @@ export class Activity { }); } - public runNoEncoding(fn: ActivityFunction, input: ActivityExecuteInput): Promise { + public runNoEncoding(fn: F, input: ActivityExecuteInput): ReturnType { if (this.fn !== undefined) throw new IllegalStateError('Activity function is defined'); - return asyncLocalStorage.run(this.context, () => this.execute(fn, input)); + return asyncLocalStorage.run(this.context, () => this.execute(fn, input)) as ReturnType; } }