1
1
import { mapToPayloads , searchAttributePayloadConverter , toPayloads } from '@temporalio/common' ;
2
2
import {
3
3
ActivityFunction ,
4
- ActivityInterface ,
5
4
ActivityOptions ,
6
5
compileRetryPolicy ,
7
6
composeInterceptors ,
@@ -14,6 +13,7 @@ import {
14
13
SearchAttributes ,
15
14
SignalDefinition ,
16
15
tsToMs ,
16
+ UntypedActivities ,
17
17
WithWorkflowArgs ,
18
18
Workflow ,
19
19
WorkflowResultType ,
@@ -117,13 +117,6 @@ export function sleep(ms: number | string): Promise<void> {
117
117
} ) ;
118
118
}
119
119
120
- export interface ActivityInfo {
121
- name : string ;
122
- type : string ;
123
- }
124
-
125
- export type InternalActivityFunction < P extends any [ ] , R > = ActivityFunction < P , R > & ActivityInfo ;
126
-
127
120
function validateActivityOptions ( options : ActivityOptions ) : void {
128
121
if ( options . scheduleToCloseTimeout === undefined && options . startToCloseTimeout === undefined ) {
129
122
throw new TypeError ( 'Required either scheduleToCloseTimeout or startToCloseTimeout' ) ;
@@ -444,6 +437,43 @@ function signalWorkflowNextHandler({ seq, signalName, args, target, headers }: S
444
437
} ) ;
445
438
}
446
439
440
+ /**
441
+ * Symbol used in the return type of proxy methods to mark that an attribute on the source type is not a method.
442
+ *
443
+ * @see {@link ActivityInterfaceFor }
444
+ * @see {@link proxyActivities }
445
+ * @see {@link proxyLocalActivities }
446
+ */
447
+ export const NotAnActivityMethod = Symbol . for ( '__TEMPORAL_NOT_AN_ACTIVITY_METHOD' ) ;
448
+
449
+ /**
450
+ * Type helper that takes a type `T` and transforms attributes that are not {@link ActivityFunction} to
451
+ * {@link NotAnActivityMethod}.
452
+ *
453
+ * @example
454
+ *
455
+ * Used by {@link proxyActivities} to get this compile-time error:
456
+ *
457
+ * ```ts
458
+ * interface MyActivities {
459
+ * valid(input: number): Promise<number>;
460
+ * invalid(input: number): number;
461
+ * }
462
+ *
463
+ * const act = proxyActivities<MyActivities>({ startToCloseTimeout: '5m' });
464
+ *
465
+ * await act.valid(true);
466
+ * await act.invalid();
467
+ * // ^ TS complains with:
468
+ * // (property) invalidDefinition: typeof NotAnActivityMethod
469
+ * // This expression is not callable.
470
+ * // Type 'Symbol' has no call signatures.(2349)
471
+ * ```
472
+ */
473
+ export type ActivityInterfaceFor < T > = {
474
+ [ K in keyof T ] : T [ K ] extends ActivityFunction ? T [ K ] : typeof NotAnActivityMethod ;
475
+ } ;
476
+
447
477
/**
448
478
* Configure Activity functions with given {@link ActivityOptions}.
449
479
*
@@ -452,11 +482,9 @@ function signalWorkflowNextHandler({ seq, signalName, args, target, headers }: S
452
482
* @return a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
453
483
* for which each attribute is a callable Activity function
454
484
*
455
- * @typeparam A An {@link ActivityInterface} - mapping of name to function
456
- *
457
485
* @example
458
486
* ```ts
459
- * import { proxyActivities, ActivityInterface } from '@temporalio/workflow';
487
+ * import { proxyActivities } from '@temporalio/workflow';
460
488
* import * as activities from '../activities';
461
489
*
462
490
* // Setup Activities from module exports
@@ -465,7 +493,7 @@ function signalWorkflowNextHandler({ seq, signalName, args, target, headers }: S
465
493
* });
466
494
*
467
495
* // Setup Activities from an explicit interface (e.g. when defined by another SDK)
468
- * interface JavaActivities extends ActivityInterface {
496
+ * interface JavaActivities {
469
497
* httpGetFromJava(url: string): Promise<string>
470
498
* someOtherJavaActivity(arg1: number, arg2: string): Promise<string>;
471
499
* }
@@ -484,7 +512,7 @@ function signalWorkflowNextHandler({ seq, signalName, args, target, headers }: S
484
512
* }
485
513
* ```
486
514
*/
487
- export function proxyActivities < A extends ActivityInterface > ( options : ActivityOptions ) : A {
515
+ export function proxyActivities < A = UntypedActivities > ( options : ActivityOptions ) : ActivityInterfaceFor < A > {
488
516
if ( options === undefined ) {
489
517
throw new TypeError ( 'options must be defined' ) ;
490
518
}
@@ -513,13 +541,11 @@ export function proxyActivities<A extends ActivityInterface>(options: ActivityOp
513
541
* @return a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
514
542
* for which each attribute is a callable Activity function
515
543
*
516
- * @typeparam A An {@link ActivityInterface} - mapping of name to function
517
- *
518
544
* @experimental
519
545
*
520
546
* See {@link proxyActivities} for examples
521
547
*/
522
- export function proxyLocalActivities < A extends ActivityInterface > ( options : LocalActivityOptions ) : A {
548
+ export function proxyLocalActivities < A = UntypedActivities > ( options : LocalActivityOptions ) : ActivityInterfaceFor < A > {
523
549
if ( options === undefined ) {
524
550
throw new TypeError ( 'options must be defined' ) ;
525
551
}
0 commit comments