---
title: "TypeScript and WebGPU: write GPU shaders in TypeScript"
description: "TypeScript host code prepares data and submits work through a browser GPU API. A shader describes the calculation each GPU invocation performs."
url: https://typeshade.dev/guide/typescript-and-webgpu/
language: en
---

# WebGPU shaders in TypeScript

TypeScript host code prepares data and submits work through a browser GPU API. A shader describes the calculation each GPU invocation performs.

## When it helps

Use this split to decide where an operation belongs. The host handles application state and resource setup; shaders process the data made available to their stage.

## Worked example

The host supplies an input array and a gain. Each shader invocation reads its own input element and writes the scaled value to a separate output element.

Example reasoning

```
input = [1, 2, 3]
gain = 2
output[i] = input[i] * gain
output = [2, 4, 6]
```

## Practical limits

TypeScript types check host code but do not submit GPU work. A shader still needs a compatible entry point, bound resources, and a draw or dispatch.

TypeShade lets you write WebGPU shaders in TypeScript. A file marked with `"use typeshade"` compiles to WGSL for WebGPU and to GLSL ES 3.00 for WebGL2. Your editor reads the same file as TypeScript. This page maps the types, functions and modules you know to what a shader needs, and shows where the WebGPU API begins.

## Start with a TypeScript file

```ts
"use typeshade";

@vertex
export function main(): vec4 {
  return vec4(0., 0., 0., 1.);
}
```

The WGSL it compiles to

```wgsl
@vertex
fn main() -> @builtin(position) vec4<f32> {
  return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
```

`"use typeshade"` marks the file as TypeShade source, and the file compiles with the TypeShade files it imports as one program. The rest of the program still uses familiar TypeScript-shaped constructs: functions, parameters, return types, objects, imports and exports. The second block is the WGSL the pinned compiler writes for this file, produced while the page is built.

## The mental model

### TypeScript types

Type annotations describe values and make incorrect programs visible to tooling.

**TypeShade adds** GPU value types such as `f32`, `u32`, `vec2`, `vec3` and `vec4`, plus rules for shader operations.

### Functions

A function has parameters, a return type and a body.

**TypeShade adds** Stage attributes such as `@vertex` and `@fragment` to identify entry points.

### Modules

`import` and `export` share code between the files of a program.

**TypeShade adds** Shader files import one another by relative paths and compile as one program, which the compiler lowers to GPU code from what its entry points reach.

### Web APIs

The host application owns devices, pipelines, buffers and rendering.

**TypeShade adds** Shader source and reflection metadata for the host's own WebGPU or WebGL2 code. A module the host imports through the Vite plugin brings a runtime that creates the device and the pipelines its calls need.

## Where WebGPU fits

Think of TypeShade as the authoring and compilation layer above the browser graphics API. TypeShade produces shader code, and the application creates a `GPUDevice`, configures a pipeline, binds resources and submits work. A module the application imports through the Vite plugin does those steps inside the calls it exports. [The shader pipeline](https://typeshade.dev/guide/concepts/the-pipeline/) shows where the entry points run, and [WebGPU and WebGL2](https://typeshade.dev/guide/concepts/webgpu-and-webgl2/) shows which side owns each object.

- [TypeScript documentation ↗](https://www.typescriptlang.org/docs/)
- [MDN WebGPU API ↗](https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API)
- [MDN JavaScript directives ↗](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
- [Try it in the Playground →](https://typeshade.dev/playground/)

## WebGPU types

Host code that calls WebGPU needs the WebGPU type declarations. With TypeScript 5, install `@webgpu/types` and add it to `compilerOptions.types` in `tsconfig.json`. TypeScript 6 carries only part of them in its `dom` library, and the [WebGPU types README](https://github.com/gpuweb/types) says what to use there. WebGL2 is in the `dom` library of both versions. The declarations of the `typeshade` package need no WebGPU types, because its runtime takes the device as an `object`.

## Learn in this order

1. Learn the TypeScript shape: types, functions and modules.
2. Add `"use typeshade"` and learn the [GPU types](https://typeshade.dev/guide/language/gpu-types/).
3. Add entry-point stage attributes and resource declarations: [Shader stages](https://typeshade.dev/guide/language/stages/) and [Resources](https://typeshade.dev/guide/language/resources/).
4. Use the Playground to see diagnostics and generated WGSL while you edit.
5. Only then move into compiler and backend details.
