---
title: "TypeShade expressions and types, and what they lower to"
description: "Assertions, destructuring, spreads, literals and the values a shader cannot build while it runs, each with the shader text or the refusal it gets."
url: https://typeshade.dev/guide/language/from-typescript/expressions/
language: en
---

# 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 `const` and `let` array 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.
  TypeScript
  ```ts
  export function swapped(v: vec2): vec2 {
    const { x, y } = v
    return vec2(y, x)
  }

  export function endpoints(xs: array<f32, 3>): f32 {
    const [first, , last] = xs
    return first + last
  }
  ```
  Emitted WGSL
  ```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
  ```glsl
  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.
  TypeScript
  ```ts
  export function lifted(p: Point): Point {
    return { ...p, y: 9. }
  }
  ```
  Emitted WGSL
  ```wgsl
  fn lifted(p: Point) -> Point {
    return Point(p.x, 9.0);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  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-ramp](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/array-literal-ramp/array-literal-ramp.shade.ts)
  TypeScript
  ```ts
  export function step3(i: i32): f32 {
    const xs: array<f32, 3> = [0.15, 0.5, 0.9]
    return xs[i]
  }
  ```
  Emitted WGSL
  ```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
  ```glsl
  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](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/tuple-and-brand/tuple-and-brand.shade.ts)
  TypeScript
  ```ts
  export function bounds(x: f32): [f32, f32] {
    return [x - 1., x + 1.]
  }
  ```
  Emitted WGSL
  ```wgsl
  fn bounds(x: f32) -> array<f32, 2> {
    return array<f32, 2>((x - 1.0), (x + 1.0));
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  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](https://typeshade.dev/guide/language/string-keys-record/). Other unions whose members name one type name that type.
  TypeScript
  ```ts
  export function isWire(m: 0 | 1 | 2): bool {
    return m === 2
  }
  ```
  Emitted WGSL
  ```wgsl
  fn isWire(m: i32) -> bool {
    return (m == 2);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  bool isWire(int m) {
    return (m == 2);
  }
  ```
- ### Branded type
  The brand is erased and the parameter is an `f32`. [tuple-and-brand](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/tuple-and-brand/tuple-and-brand.shade.ts)
  TypeScript
  ```ts
  type Metres = f32 & { readonly [m]: 'metres' }
  ```
  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);
  }
  ```
- ### Type claims
  A claim about a type, not a conversion, so each emits what its operand emits.
  TypeScript
  ```ts
  export function claimed(x: f32): f32 {
    const a = x as f32
    const b = (x satisfies f32)!
    const k = 2. as const
    return a + b * k
  }
  ```
  Emitted WGSL
  ```wgsl
  fn claimed(x: f32) -> f32 {
    return (x + (x * 2.0));
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float claimed(float x) {
    return (x + (x * 2.0));
  }
  ```

## Operators

- ### Exponent operator
  `pow` on both targets.
  TypeScript
  ```ts
  export function curved(x: f32): f32 {
    return x ** 2.2
  }
  ```
  Emitted WGSL
  ```wgsl
  fn curved(x: f32) -> f32 {
    return pow(x, 2.2);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float curved(float x) {
    return pow(x, 2.2);
  }
  ```
- ### `&&` on a `bool`
  The operator both targets have, on one `bool` at a time.
  TypeScript
  ```ts
  export function both(a: bool, b: bool): bool {
    return a && b
  }
  ```
  Emitted WGSL
  ```wgsl
  fn both(a: bool, b: bool) -> bool {
    return (a && b);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  bool both(bool a, bool b) {
    return (a && b);
  }
  ```
- ### `&&` on a mask
  Refused
  Combine masks with `all`, `any` or a `select`. [bool-select](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/bool-select/bool-select.shade.ts)
  TypeScript
  ```ts
  export function both(a: vec2b, b: vec2b): bool {
    return a && b
  }
  ```
  Compiler diagnostic
  **[TS8003](https://typeshade.dev/reference/errors/ts8003/)** Logical "&&" requires bool operands.
- ### Optional field
  A field `radius?: f32 | null` admits null and undefined; omitting it supplies undefined ([Rule 8.8](https://typeshade.dev/reference/rules/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 values](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/null-values/null-values.shade.ts)
  TypeScript
  ```ts
  class Disc {
    center: vec2
    radius?: f32
  }
  ```
  Emitted WGSL
  ```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
  ```glsl
  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
  Refused
  A string literal is accepted where a [key type](https://typeshade.dev/guide/language/string-keys-record/) is expected. Open `string` stays refused.
  TypeScript
  ```ts
  export function label(x: f32): f32 {
    const name = `band ${x}`
    return x
  }
  ```
  Compiler diagnostic
  **[TS8013](https://typeshade.dev/reference/errors/ts8013/)** Template strings are JS. TypeShade has no string type.
- ### `number`
  A `number` written as a type is inferred from its uses, and is an `f32` where no use decides. Write `f32`, `i32` or `u32` to fix a width.
  TypeScript
  ```ts
  export function twice(x: number): number {
    return x * 2
  }
  ```
  Emitted WGSL
  ```wgsl
  fn twice(x: f32) -> f32 {
    return (x * 2.0);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float twice(float x) {
    return (x * 2.0);
  }
  ```
- ### `boolean`
  A `boolean` written as a type is a `bool`.
  TypeScript
  ```ts
  export function on(x: boolean): f32 {
    return x ? 1. : 0.
  }
  ```
  Emitted WGSL
  ```wgsl
  fn on(x: bool) -> f32 {
    return select(0.0, 1.0, x);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  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 `let` initialized by one, as `let n = -1` is, 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, a `return` in 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 an `f32`. Uses that ask for `i32` and for `u32`, or an integer use beside a float use, produce [`TS8003`](https://typeshade.dev/reference/errors/ts8003/); an annotation settles the type. A `/` whose operands are integers only by inference is refused too ([`TS8003`](https://typeshade.dev/reference/errors/ts8003/)), so that the meaning of a division does not depend on another line. Float-written initializers stay `f32`, a `const` of written numbers takes the type of each place that reads it, and the counter of a counted `for` keeps the type of [Rule 7.5](https://typeshade.dev/reference/rules/7-5/).
  TypeScript
  ```ts
  class 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
  ```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
  ```glsl
  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.
  TypeScript
  ```ts
  export function stepped(x: f32): f32 {
    let acc: f32 = x
    acc++
    acc--
    return acc
  }
  ```
  Emitted WGSL
  ```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
  ```glsl
  float stepped(float x) {
    float acc = x;
    acc = (acc + 1.0);
    acc = (acc - 1.0);
    return acc;
  }
  ```
