Declarations
On this page

Declarations

A declaration names a constant, a variable or a layout. Which of the three it is decides whether anything is emitted for it at all.

Constants and variables

  • Scalar const

    A module constant. A scalar folds to one value at its declaration. module-const

    TypeScript
    const GAIN: f32 = 1.5 * 2.
    Emitted WGSL
    const GAIN: f32 = 3.0;
    Emitted GLSL ES 3.00
    const float GAIN = 3.0;
  • Vector const

    A vector or array constant carries its value as an expression each backend evaluates. palette-const

    TypeScript
    const TINT: vec3 = vec3(0.2, 0.4, 0.9)
    Emitted WGSL
    const TINT: vec3<f32> = vec3<f32>(0.2, 0.4, 0.9);
    Emitted GLSL ES 3.00
    const vec3 TINT = vec3(0.2, 0.4, 0.9);
  • Local let

    A mutable local. The annotation carries the type, and every target starts it at zero. bitfield-bands

    TypeScript
    export function band(x: f32): f32 {
    let w: f32
    w = x * 2.
    return w
    }
    Emitted WGSL
    fn band(x: f32) -> f32 {
    let w = (x * 2.0);
    return w;
    }
    Emitted GLSL ES 3.00
    float band(float x) {
    float w = (x * 2.0);
    return w;
    }
  • Read before assignment

    Refused

    A read of a local on a path that has not assigned it is refused with TS8075, by the rule TypeScript reports as TS2454. The editor shows TS8075 in its place.

    TypeScript
    export function band(x: f32): f32 {
    let w: f32
    if (x > 0.) {
    w = x * 2.
    }
    return w
    }

    Compiler diagnostic

    TS8075 "w" is read here before it is assigned on every path. Assign it before this read, or declare it with a value (Rule 7.6).

  • Module let

    A module variable, one per invocation. private-state

    TypeScript
    let seed: u32 = 7
    Emitted WGSL
    var<private> seed: u32 = 7u;
    Emitted GLSL ES 3.00
    uint seed = 7u;
  • var

    Refused

    let is the per-invocation variable and const the module constant.

    TypeScript
    var k = 1.

    Compiler diagnostic

    TS8014 Top-level var is not allowed. Use `let` for a per-invocation variable or `const` for a module constant.

Types and namespaces

  • enum

    One module constant per member, named Enum_Member and typed i32.

    TypeScript
    enum Mode {
    Flat,
    Shaded,
    Wire,
    }
    Emitted WGSL
    const Mode_Flat: i32 = 0;
    const Mode_Shaded: i32 = 1;
    const Mode_Wire: i32 = 2;
    Emitted GLSL ES 3.00
    const int Mode_Flat = 0;
    const int Mode_Shaded = 1;
    const int Mode_Wire = 2;
  • const enum

    The same constants. A const enum is no different here.

    TypeScript
    const enum Flag {
    None = 0,
    Both = 3,
    }
    Emitted WGSL
    const Flag_None: i32 = 0;
    const Flag_Both: i32 = 3;
    Emitted GLSL ES 3.00
    const int Flag_None = 0;
    const int Flag_Both = 3;
  • type alias

    Another name for its target, resolved wherever a type may stand.

    TypeScript
    type Meters = f32
    Emitted WGSL
    fn deeper(d: f32) -> f32 {
    return (d * 2.0);
    }
    Emitted GLSL ES 3.00
    float deeper(float d) {
    return (d * 2.0);
    }
  • interface

    A struct, the same one a class of those fields would give.

    TypeScript
    interface Camera {
    view: mat4
    pos: vec3
    }
    Emitted WGSL
    struct Camera {
    view: mat4x4<f32>,
    pos: vec3<f32>,
    }
    Emitted GLSL ES 3.00
    struct Camera {
    mat4 view;
    vec3 pos;
    };
  • Class as a struct

    A struct, whose fields lay out the memory the host fills. ray-class

    TypeScript
    class Ray {
    origin: vec3
    dir: vec3
    }
    Emitted WGSL
    struct Ray {
    origin: vec3<f32>,
    dir: vec3<f32>,
    }
    Emitted GLSL ES 3.00
    struct Ray {
    vec3 origin;
    vec3 dir;
    };
  • namespace

    The members flatten to Ns_member, and the namespace itself emits nothing.

    TypeScript
    namespace Palette {
    export const WARM: vec3 = vec3(0.9, 0.6, 0.3)
    export function tint(x: f32): vec3 {
    return WARM * x
    }
    }
    Emitted WGSL
    const Palette_WARM: vec3<f32> = vec3<f32>(0.9, 0.6, 0.3);
    fn Palette_tint(x: f32) -> vec3<f32> {
    return (Palette_WARM * x);
    }
    Emitted GLSL ES 3.00
    const vec3 Palette_WARM = vec3(0.9, 0.6, 0.3);
    vec3 Palette_tint(float x) {
    return (Palette_WARM * x);
    }

Edit this page Report a problem