Types
On this page

Types: from TypeScript types to GPU values

TypeShade starts from TypeScript’s type surface, but the final meaning is a GPU value model. Learn the TypeScript idea first: structural typing and type aliases describe value shape. When GPU layout metadata is required, write it in the field’s type: `builtin<N>` and `location<n, T>`.

Start from the TypeScript concept

A type alias gives a name to a value shape. TypeShade keeps that surface, while shader semantics determine which types and expressions are valid in a shader.

1. Use type aliases for plain data

When fields need no attribute, a type alias is the smallest representation. It is also useful when the same GPU value shape is shared across helper parameters and return values.

type Camera = {
view: mat4
pos: vec3
}

2. A class is a struct and its functions

A TypeShade class is a GPU struct and the functions written with it. The fields are the bytes the host writes. A constructor, a method and a static function each lower to a plain function, so new Ray(o, d) calls Ray_new and r.at(t) calls Ray_at(&r, t) on WGSL. The method takes its object by pointer and reads it in place, with no copy of the struct, whether r is a let or a const (Rule 8.28). GLSL ES 3.00 passes a local to a method that only reads it by value, and to a method that writes its object as an inout parameter. Nothing keeps an object alive between them. A class is its struct and its functions (Rule 8.29): every rule for a struct applies to its fields and every rule for a function to its constructor, its methods, its accessors and its static functions, wherever a struct or a function can be. A method that changes its object, called on an element of the array a kernel function takes, is a write to that element, as an argument of a parameter that its function writes is.

class VsIn {
position: vec3
uv: vec2
}

3. Attribute types describe layout

A field of an entry input or output says how it binds to the pipeline in its type. A built-in value is builtin<N>, whose type WGSL gives. Any other field is user-defined: the compiler gives it a location, and location<n, T> states the number. An object type of options qualifies a value: location<1, u32, { interpolate: "flat" }>, builtin<"position", { invariant: true }> or location<0, vec4, { blend_src: 1 }>. A class field needs no initializer and no !. The decorated form these types replace, @location(0) and the rest, still compiles with a TS8028 warning until the release that refuses it.

class VsOut {
position: builtin<"position">
uv: location<0, vec2>
}

4. Where TypeScript classes stop

  • An entry point is a top-level function, not a method.
  • A class with no instance fields can be constructed and can have methods, accessors and static members. Its host value is {}, and a result is an instance of the class when the module exports it (Rule 8.21). Reflection includes the internal GPU storage footprint: 4 bytes in std430 and 16 bytes in std140, with no authored field added.
  • A derived value can be used where its base class is declared when the compiler proves that the base view is read-only and preserves dispatch. Overrides, receiver writes and observable alias mutation require a concrete type or remain unsupported.
  • A getter and a setter each lower to a function of their own, and a class has one constructor, as in TypeScript.
  • new builds a value inside a function body, and a module constant takes an object literal.
  • Prefer a type alias or an interface when no field needs an initializer.
  • A value may be absent as T | undefined, T | null or T | null | undefined in function-scope locals, parameters, results, class fields and fixed-array elements (Rules 7.10 and 8.8). Strict tests distinguish null and undefined; != null excludes both. Bindings, module/static data, entry IO and kernel interfaces refuse these types. Exported helpers using them have no host value (Rule 8.21).

5. Concept mapping

TypeScriptTypeShade
type alias / object shapeGPU value shape
class fieldsGPU struct fields
class methodA function taking the struct first
newA call of the generated constructor
extends with superThe base fields spliced in and the body lowered again
attribute types and stage attributesGPU layout / stage metadata
runtime objectnot applicable
structural compatibilityapplies within shader type checking

6. Connect the type to an entry point

After defining a struct, use it as an entry point parameter. The value shape and field metadata then define the meaning of that shader input.

interface VsIn {
position: location<0, vec3>
}
@vertex
export function vs(vin: VsIn): vec4 {
return vec4(vin.position, 1)
}

Next: GPU types

Edit this page Report a problem