scook
scook
CDCloudflare Developers
Created by scook on 3/10/2025 in #workers-help
Workers service binding Typescript types uses Rpc.Stub<null>
Hi team, I cannot get TS return types working as expected when calling a method via service binding between Workers. My goal is to have the service return in the form:
type ServiceResponseSuccess<T> = {
data: T;
error: null;
};
type ServiceResponseFailure = {
data: null;
error: MyErrorObject;
};
export type ServiceResponse<T> = ServiceResponseFailure | ServiceResponseSuccess<T>;


// my-service.ts:
async myMethod(...): Promise<ServiceResponse<boolean>>;
type ServiceResponseSuccess<T> = {
data: T;
error: null;
};
type ServiceResponseFailure = {
data: null;
error: MyErrorObject;
};
export type ServiceResponse<T> = ServiceResponseFailure | ServiceResponseSuccess<T>;


// my-service.ts:
async myMethod(...): Promise<ServiceResponse<boolean>>;
and then the caller can do:
const { data, error } = await myService.myMethod(...);
const { data, error } = await myService.myMethod(...);
The issue is that the generated TS types for the service use Rpc.Stub<null> instead of raw null. And this does not play nice with anything the caller would want to do afterwards. For example, if (error) does not work to check if we're in the error case, since Rpc.Stub<null> != null; and the same null-check doesn't work for the data object.
// generated TS return type for the service
Promise<{ data: Rpc.Stub<null>; error: {...}; }>
| Promise<{ data: {...}; error: Rpc.Stub<null>; }>
// generated TS return type for the service
Promise<{ data: Rpc.Stub<null>; error: {...}; }>
| Promise<{ data: {...}; error: Rpc.Stub<null>; }>
Btw, it appears to only use Rpc.Stub<> for null and undefined. For all other types (number, boolean, custom types, etc) , the generated TS uses the actual type.
1 replies
CDCloudflare Developers
Created by scook on 10/21/2024 in #workers-help
Workers duration limit confusion
Use event.waitUntil() to delay cancellation for another 30 seconds or until the promise passed to waitUntil() completes.
The docs on Duration Limit (https://developers.cloudflare.com/workers/platform/limits/#duration) says that using waitUntil() delays Worker expiration untils 30 secs *or * the promise completes. The "or" is confusing me. Does that mean it'll continue for the longer of the two (and potentially indefinitely while awaiting the Promise)?
3 replies