Skip to content

Missing "webgpu" getContext overloads for HTMLCanvasElement and OffscreenCanvas #2508

Description

@Srinuyadav149

Summary

HTMLCanvasElement.getContext("webgpu) and OffscreenCanvas.getContext("webgpu") are missing overloads and therefore resolving to the generic getContext overloads instead of returning GPUCanvasContext. GPUCanvasContext is already present in the generated DOM types. But additionalSignatures entries in inputfiles/overriding types.jsonc only include "2d", "bitmaprenderer", "webgl", "webgl2". As a result, WebGPU Canvas contexts losing it's type specific info.

Expected vs. Actual Behavior

Expected Behaviour

const canvas = document.createElement("canvas")
const context = canvas.getContext("webgpu")

context should be of type
GPUCanvasContext
Similarly for

const canvas = new OffscreenCanvas(300, 300)
const context = canvas.getContext("webgpu")

context should be of type
GPUCanvasContext

Actual Behaviour

const canvas = document.createElement("canvas")
const context = canvas.getContext("webgpu")

context is of type
RenderingContext
Similarly for

const canvas = new OffscreenCanvas(300, 300)
const context = canvas.getContext("webgpu")

context is of type
RenderingContext

because getContext("webgpu") falling back to generic overload
getContext(contexId: string, options?: any): RenderingContent | null;

This is preventing Typescript from recognising WebGPU-specific API's such as
context?.configure()

Playground Link

No response

Browser Support

  • This API is supported in at least two major browser engines (not two Chromium-based browsers).

Have Tried The Latest Releases

  • This issue applies to the latest release of TypeScript.
  • This issue applies to the latest release of @types/web.

Additional Context

The relevant overloads are manually defined in inputfiles/overridingTypes.jsonc via additionalSignatures.

Currently additionalSignatures for:

  • "2d"
  • "bitmaprenderer"
  • "webgl"
  • "webgl2"

These are present for both HTMLCanvasElement and OffscreenCanvas, but webgpu is missing from the additionalSignatures.

Adding the following overload locally in lib.dom.d.ts resolves the issue:

getContext(context is: "webgpu"): GPUCanvasContext | null

Can I create a pull request adding missing "webgpu" overloads to these additionalSignatures entries in overridingTypes.jsonc file?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions