---
title: "Declarations in TypeShade and what they lower to"
description: "What a constant, a variable, an enum, a type alias, an interface and a namespace become in a \"use typeshade\" file, and what is emitted for each."
url: https://typeshade.dev/guide/language/from-typescript/declarations/
language: en
---

# 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](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/module-const/module-const.shade.ts)
  TypeScript
  ```ts
  const GAIN: f32 = 1.5 * 2.
  ```
  Emitted WGSL
  ```wgsl
  const GAIN: f32 = 3.0;
  ```
  Emitted GLSL ES 3.00
  ```glsl
  const float GAIN = 3.0;
  ```
- ### Vector `const`
  A vector or array constant carries its value as an expression each backend evaluates. [palette-const](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/palette-const/palette-const.shade.ts)
  TypeScript
  ```ts
  const TINT: vec3 = vec3(0.2, 0.4, 0.9)
  ```
  Emitted WGSL
  ```wgsl
  const TINT: vec3<f32> = vec3<f32>(0.2, 0.4, 0.9);
  ```
  Emitted GLSL ES 3.00
  ```glsl
  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](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/bitfield-bands/bitfield-bands.shade.ts)
  TypeScript
  ```ts
  export function band(x: f32): f32 {
    let w: f32
    w = x * 2.
    return w
  }
  ```
  Emitted WGSL
  ```wgsl
  fn band(x: f32) -> f32 {
    let w = (x * 2.0);
    return w;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  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`](https://typeshade.dev/reference/errors/ts8075/), by the rule TypeScript reports as TS2454. The editor shows [`TS8075`](https://typeshade.dev/reference/errors/ts8075/) in its place.
  TypeScript
  ```ts
  export function band(x: f32): f32 {
    let w: f32
    if (x > 0.) {
      w = x * 2.
    }
    return w
  }
  ```
  Compiler diagnostic
  **[TS8075](https://typeshade.dev/reference/errors/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](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/private-state/private-state.shade.ts)
  TypeScript
  ```ts
  let seed: u32 = 7
  ```
  Emitted WGSL
  ```wgsl
  var<private> seed: u32 = 7u;
  ```
  Emitted GLSL ES 3.00
  ```glsl
  uint seed = 7u;
  ```
- ### `var`
  Refused
  `let` is the per-invocation variable and `const` the module constant.
  TypeScript
  ```ts
  var k = 1.
  ```
  Compiler diagnostic
  **[TS8014](https://typeshade.dev/reference/errors/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
  ```ts
  enum Mode {
    Flat,
    Shaded,
    Wire,
  }
  ```
  Emitted WGSL
  ```wgsl
  const Mode_Flat: i32 = 0;

  const Mode_Shaded: i32 = 1;

  const Mode_Wire: i32 = 2;
  ```
  Emitted GLSL ES 3.00
  ```glsl
  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
  ```ts
  const enum Flag {
    None = 0,
    Both = 3,
  }
  ```
  Emitted WGSL
  ```wgsl
  const Flag_None: i32 = 0;

  const Flag_Both: i32 = 3;
  ```
  Emitted GLSL ES 3.00
  ```glsl
  const int Flag_None = 0;

  const int Flag_Both = 3;
  ```
- ### `type` alias
  Another name for its target, resolved wherever a type may stand.
  TypeScript
  ```ts
  type Meters = f32
  ```
  Emitted WGSL
  ```wgsl
  fn deeper(d: f32) -> f32 {
    return (d * 2.0);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float deeper(float d) {
    return (d * 2.0);
  }
  ```
- ### `interface`
  A struct, the same one a class of those fields would give.
  TypeScript
  ```ts
  interface Camera {
    view: mat4
    pos: vec3
  }
  ```
  Emitted WGSL
  ```wgsl
  struct Camera {
    view: mat4x4<f32>,
    pos: vec3<f32>,
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  struct Camera {
    mat4 view;
    vec3 pos;
  };
  ```
- ### Class as a struct
  A struct, whose fields lay out the memory the host fills. [ray-class](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/ray-class/ray-class.shade.ts)
  TypeScript
  ```ts
  class Ray {
    origin: vec3
    dir: vec3
  }
  ```
  Emitted WGSL
  ```wgsl
  struct Ray {
    origin: vec3<f32>,
    dir: vec3<f32>,
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  struct Ray {
    vec3 origin;
    vec3 dir;
  };
  ```
- ### `namespace`
  The members flatten to `Ns_member`, and the namespace itself emits nothing.
  TypeScript
  ```ts
  namespace Palette {
    export const WARM: vec3 = vec3(0.9, 0.6, 0.3)
    export function tint(x: f32): vec3 {
      return WARM * x
    }
  }
  ```
  Emitted WGSL
  ```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
  ```glsl
  const vec3 Palette_WARM = vec3(0.9, 0.6, 0.3);

  vec3 Palette_tint(float x) {
    return (Palette_WARM * x);
  }
  ```
