Expressions and types
A type claim is erased, a shape the GPU has a word for is kept, and a value TypeScript would build at run time is refused where it is written.
Objects, arrays and tuples
-
Destructuring
Object patterns read fields. Local
constandletarray patterns copy elements of fixed-length arrays and homogeneous tuples in source order. Omissions consume positions; nested patterns are supported and the source is evaluated once. Bounds, element types and readonly access still apply. Rest elements, defaults, destructuring assignment, parameters, vector array patterns and arrays of runtime or specialization-dependent length are refused.TypeScriptexport function swapped(v: vec2): vec2 {const { x, y } = vreturn vec2(y, x)}export function endpoints(xs: array<f32, 3>): f32 {const [first, , last] = xsreturn first + last}Emitted WGSL fn swapped(v: vec2<f32>) -> vec2<f32> {let x = v.x;let y = v.y;return vec2<f32>(y, x);}fn endpoints(xs: array<f32, 3>) -> f32 {let first = xs[0u];let last = xs[2u];return (first + last);}Emitted GLSL ES 3.00 vec2 swapped(vec2 v) {float x = v.x;float y = v.y;return vec2(y, x);}float endpoints(float[3] xs) {float first = xs[0u];float last = xs[2u];return (first + last);} -
Object spread
One read per field of the struct, with the fields written after it over them.
TypeScriptexport function lifted(p: Point): Point {return { ...p, y: 9. }}Emitted WGSL fn lifted(p: Point) -> Point {return Point(p.x, 9.0);}Emitted GLSL ES 3.00 Point lifted(Point p) {return Point(p.x, 9.0);} -
Array literal
An array initializer with a contextual element type and length, such as
array<T, N>. A local array binding pattern can also infer a homogeneous literal initializer. array-literal-rampTypeScriptexport function step3(i: i32): f32 {const xs: array<f32, 3> = [0.15, 0.5, 0.9]return xs[i]}Emitted WGSL fn step3(i: i32) -> f32 {var xs: array<f32, 3> = array<f32, 3>(0.15, 0.5, 0.9);return xs[i];}Emitted GLSL ES 3.00 float step3(int i) {float[3] xs = float[3](0.15, 0.5, 0.9);return xs[i];} -
Tuple
A tuple is an array of a length the type fixes, a return included. tuple-and-brand
TypeScriptexport function bounds(x: f32): [f32, f32] {return [x - 1., x + 1.]}Emitted WGSL fn bounds(x: f32) -> array<f32, 2> {return array<f32, 2>((x - 1.0), (x + 1.0));}Emitted GLSL ES 3.00 float[2] bounds(float x) {return float[2]((x - 1.0), (x + 1.0));}
Type claims
-
Literal union
A string-literal union is a key type. Other unions whose members name one type name that type.
TypeScriptexport function isWire(m: 0 | 1 | 2): bool {return m === 2}Emitted WGSL fn isWire(m: i32) -> bool {return (m == 2);}Emitted GLSL ES 3.00 bool isWire(int m) {return (m == 2);} -
Branded type
The brand is erased and the parameter is an
f32. tuple-and-brandTypeScripttype Metres = f32 & { readonly [m]: 'metres' }Emitted WGSL fn deeper(d: f32) -> f32 {return (d * 2.0);}Emitted GLSL ES 3.00 float deeper(float d) {return (d * 2.0);} -
Type claims
A claim about a type, not a conversion, so each emits what its operand emits.
TypeScriptexport function claimed(x: f32): f32 {const a = x as f32const b = (x satisfies f32)!const k = 2. as constreturn a + b * k}Emitted WGSL fn claimed(x: f32) -> f32 {return (x + (x * 2.0));}Emitted GLSL ES 3.00 float claimed(float x) {return (x + (x * 2.0));}
Operators
-
Exponent operator
powon both targets.TypeScriptexport function curved(x: f32): f32 {return x ** 2.2}Emitted WGSL fn curved(x: f32) -> f32 {return pow(x, 2.2);}Emitted GLSL ES 3.00 float curved(float x) {return pow(x, 2.2);} -
&&on aboolThe operator both targets have, on one
boolat a time.TypeScriptexport function both(a: bool, b: bool): bool {return a && b}Emitted WGSL fn both(a: bool, b: bool) -> bool {return (a && b);}Emitted GLSL ES 3.00 bool both(bool a, bool b) {return (a && b);} -
Refused&&on a maskCombine masks with
all,anyor aselect. bool-selectTypeScriptexport function both(a: vec2b, b: vec2b): bool {return a && b}Compiler diagnostic
TS8003 Logical "&&" requires bool operands.
-
Optional field
A field
radius?: f32 | nulladmits null and undefined; omitting it supplies undefined (Rule 8.8). Constructors, methods, accessors, private/protected/readonly fields and generic classes support nullable payloads. An uninitialized null-only class field starts null; one admitting undefined starts undefined. A null-only local must be assigned before reading. Static data, bindings and entry IO refuse optional storage. Nullable valuesTypeScriptclass Disc {center: vec2radius?: f32}Emitted WGSL fn cover(d: Disc, p: vec2<f32>) -> f32 {return (length((p - d.center)) - select(1.0, d.radius.value, d.radius.present));}Emitted GLSL ES 3.00 float cover(Disc d, vec2 p) {return (length((p - d.center)) - mix(1.0, d.radius.value, d.radius.present));}
Strings, numbers and booleans
-
String value
RefusedA string literal is accepted where a key type is expected. Open
stringstays refused.TypeScriptexport function label(x: f32): f32 {const name = `band ${x}`return x}Compiler diagnostic
TS8013 Template strings are JS. TypeShade has no string type.
-
numberA
numberwritten as a type is inferred from its uses, and is anf32where no use decides. Writef32,i32oru32to fix a width.TypeScriptexport function twice(x: number): number {return x * 2}Emitted WGSL fn twice(x: f32) -> f32 {return (x * 2.0);}Emitted GLSL ES 3.00 float twice(float x) {return (x * 2.0);} -
booleanA
booleanwritten as a type is abool.TypeScriptexport function on(x: boolean): f32 {return x ? 1. : 0.}Emitted WGSL fn on(x: bool) -> f32 {return select(0.0, 1.0, x);}Emitted GLSL ES 3.00 float on(bool x) {return mix(0.0, 1.0, x);} -
Integer literal
A number written as an integer takes the type that the places reading it ask for. An unannotated
letinitialized by one, aslet n = -1is, takes its type from every place it reaches: an operand beside a typed peer, an argument of a declared function, constructor or method, an index, areturnin a function with a declared result, or an assignment from a typed value. Module variables, static fields that code writes and class fields initialized by integer-written numbers are read the same way. Where no use decides, the value is anf32. Uses that ask fori32and foru32, or an integer use beside a float use, produceTS8003; an annotation settles the type. A/whose operands are integers only by inference is refused too (TS8003), so that the meaning of a division does not depend on another line. Float-written initializers stayf32, aconstof written numbers takes the type of each place that reads it, and the counter of a countedforkeeps the type of Rule 7.5.TypeScriptclass Hit {constructor(public index: i32) {}offset(value: i32): i32 { return this.index + value; }}export function counts(): i32 {let n = -1;const hit = new Hit(n);n = hit.index;return hit.offset(n);}Emitted WGSL fn Hit_new_into(_dst: ptr<function, Hit>, index: i32) {(*_dst) = Hit(index);}fn Hit_offset(self_: ptr<function, Hit>, value: i32) -> i32 {return ((*self_).index + value);}fn counts() -> i32 {var n: i32 = -1;var hit: Hit;Hit_new_into(&hit, n);n = hit.index;return Hit_offset(&hit, n);}Emitted GLSL ES 3.00 Hit Hit_new(int index) {return Hit(index);}int Hit_offset(Hit self_, int value) {return (self_.index + value);}int counts() {int n = -1;Hit hit = Hit_new(n);n = hit.index;return Hit_offset(hit, n);} -
Increment and decrement
An assignment of the value one step on.
TypeScriptexport function stepped(x: f32): f32 {let acc: f32 = xacc++acc--return acc}Emitted WGSL fn stepped(x: f32) -> f32 {var acc: f32 = x;acc = (acc + 1.0);acc = (acc - 1.0);return acc;}Emitted GLSL ES 3.00 float stepped(float x) {float acc = x;acc = (acc + 1.0);acc = (acc - 1.0);return acc;}