diff --git a/.changeset/generic-sandbox-types.md b/.changeset/generic-sandbox-types.md new file mode 100644 index 00000000..b72bee8b --- /dev/null +++ b/.changeset/generic-sandbox-types.md @@ -0,0 +1,5 @@ +--- +'@cloudflare/sandbox': patch +--- + +Fix type error when extending Sandbox class by making getSandbox and SandboxEnv generic diff --git a/packages/sandbox/src/request-handler.ts b/packages/sandbox/src/request-handler.ts index a80991ac..602152d4 100644 --- a/packages/sandbox/src/request-handler.ts +++ b/packages/sandbox/src/request-handler.ts @@ -3,8 +3,8 @@ import { createLogger, type LogContext, TraceContext } from '@repo/shared'; import { getSandbox, type Sandbox } from './sandbox'; import { sanitizeSandboxId, validatePort } from './security'; -export interface SandboxEnv { - Sandbox: DurableObjectNamespace; +export interface SandboxEnv = Sandbox> { + Sandbox: DurableObjectNamespace; } export interface RouteInfo { @@ -14,10 +14,10 @@ export interface RouteInfo { token: string; } -export async function proxyToSandbox( - request: Request, - env: E -): Promise { +export async function proxyToSandbox< + T extends Sandbox, + E extends SandboxEnv +>(request: Request, env: E): Promise { // Create logger context for this request const traceId = TraceContext.fromHeaders(request.headers) || TraceContext.generate(); diff --git a/packages/sandbox/src/sandbox.ts b/packages/sandbox/src/sandbox.ts index b5014691..e235f0b1 100644 --- a/packages/sandbox/src/sandbox.ts +++ b/packages/sandbox/src/sandbox.ts @@ -45,11 +45,11 @@ import { import type { MountInfo } from './storage-mount/types'; import { SDK_VERSION } from './version'; -export function getSandbox( - ns: DurableObjectNamespace, +export function getSandbox>( + ns: DurableObjectNamespace, id: string, options?: SandboxOptions -): Sandbox { +): T { const sanitizedId = sanitizeSandboxId(id); const effectiveId = options?.normalizeId ? sanitizedId.toLowerCase() @@ -65,7 +65,7 @@ export function getSandbox( ); } - const stub = getContainer(ns, effectiveId) as unknown as Sandbox; + const stub = getContainer(ns, effectiveId); stub.setSandboxName?.(effectiveId, options?.normalizeId); @@ -87,7 +87,7 @@ export function getSandbox( return Object.assign(stub, { wsConnect: connect(stub) - }); + }) as T; } export function connect(stub: {