fix(desktop) type declarations for getContext on raw desktop backend - #36182
fix(desktop) type declarations for getContext on raw desktop backend#36182Giesch wants to merge 5 commits into
Conversation
cd9632b to
a3263fc
Compare
…dowSurface.getContext
Mirrors the overload set already declared on OffscreenCanvas.getContext,
so getContext("webgpu") narrows to GPUCanvasContext | null instead of
OffscreenRenderingContext | null. The runtime (ext/canvas/byow.rs)
supports exactly the bitmap and webgpu contexts.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…enderingContext Without a construct signature on the constructor objects, `x instanceof GPUCanvasContext` fails with TS2359 even though it works at runtime. Matches the @webgpu/types and lib.dom convention. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
a3263fc to
33dbe98
Compare
|
Checked this against the implementation and the current canary — the change is correct and the test is meaningful:
A few things worth tightening: 1. The Related, and out of scope for this PR: on canary 2.9.3 2. Mirror the // Spec also defines "2d", "webgl", and "webgl2" context ids; Deno does
// not implement those and getContext returns null for them.
getContext(contextId: "2d" | "webgl" | "webgl2", options?: any): null;The runtime behaves identically for 3. type Equals<A, B> = (<T>() => T extends A ? 1 : 2) extends
(<T>() => T extends B ? 1 : 2) ? true : false;Not blocking — the test as written does catch the regression. 4. Nit: Scope: the |
…ndowSurface Adds the doc comment and the "2d" | "webgl" | "webgl2" overload that OffscreenCanvas.getContext already declares, so the two declarations read identically. ext/canvas/byow.rs::get_context returns Ok(None) for any unrecognized context id, so the null return is accurate. Also rewords the GPUCanvasContext / ImageBitmapRenderingContext constructor object docs, which said these are obtained from getContext "rather than constructed directly" and so contradicted the new construct signature. The signature is kept because it is what makes instanceof type check and narrow, and is now documented as not being a supported construction path. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Replaces the assertType<T>(value) helper, which only checked assignability, with a strict type-equality assertion. Each call is captured in a const and queried with typeof so the selected overload's exact return type is pinned; a wrong return type now fails with TS2344. Also drops the redundant "exitCode": 0 from __test__.jsonc. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This updates the declared types on getContext to match the way they're used in the documentation (returning a
GPUCanvasContextforcontextId: "webgpu").Fixes #36214
I used Claude Code while working on this PR
Edited (2026-08-01):
This PR also adds
new ()construct signatures to theGPUCanvasContextandImageBitmapRenderingContextconstructor objects, soinstanceoftype checks and narrows.Without a construct signature,
x instanceof GPUCanvasContextraisesTS2359; the alternativeFunction & { prototype: X }clearsTS2359but narrows to{}rather thanX. This is asecond, independent type-level fix that also benefits
OffscreenCanvasusers. The constructsignature is documented as not being a supported way to create a context.