---
title: "Rule 5.1: Literals and typing, a TypeShade design rule"
description: "Rule 5.1 of the TypeShade language design, in Literals and typing. An integer-written literal must take the integer type the position around it declares."
url: https://typeshade.dev/reference/rules/5-1/
language: en
---

# Rule 5.1

Chapter 5, Literals and typing Test

An integer-written literal must take the integer type the position around it declares. An unannotated function-local `let` initialized by such a literal, by a constant of no fixed type that is one, or by `+`, `-`, `*` and `/` over those, and an unannotated module variable, static field that code writes (surface §24, [Rule 8.13](https://typeshade.dev/reference/rules/8-13/)) or class field ([Rule 8.14](https://typeshade.dev/reference/rules/8-14/)) initialized by integer-written literals and `+`, `-`, `*` and `/` over them, must take its type from every place it reaches: an operand of an arithmetic operator, a comparison or an equality beside a typed peer; an argument of a builtin beside a typed argument; an argument of a vector, matrix or array constructor; an arm of a conditional or of `select`; a field of an object literal; an operand of a bitwise or shift operator; an index; a `switch` selector; an assignment or a compound assignment from a typed value; a `return` in a function whose result type is declared; an argument of a declared parameter; the uses of a loop counter that starts at it; and another such value. An unannotated `const` or `let`, in a function or at module scope, initialized by a list of integer-written literals and `+`, `-`, `*` and `/` over them is such a value too, and its element type must come from the places its elements reach, as a `let`’s do, and from the places the whole array reaches: an element read (`a[i]`), an argument of a declared `array<T, N>` parameter, an assignment to a typed array, a `return` in a function whose result type is declared, and the array of `for (const x of a)`, whose variable is an element read. Its length is the number of its elements. Where no use decides, the element type must be `f32`, the compiler’s choice. Uses that require `i32` and `u32`, and an integer use beside a float use, must be refused as they are for a `let`, with the annotation remedy, and a `/` over an element that its uses make an integer must be refused as the `/` over a `let` is. A bitwise operand with no typed peer, a shift, an index and a `switch` selector ask for an integer of no kind, which is `i32` unless another use says `u32` or a written operand does not fit an `i32`. The compiler must solve all values of the module together, so that the result does not depend on source order. Resolve the local, callee and receiver by their declarations, including through nested scopes and inherited members; an override supplies its own signature. Uses that require `i32` and `u32` must be refused with the annotation remedy, and an integer use beside a float use must be refused where the float use stands. Where no use decides, the value must be an `f32`: that is the compiler’s choice, and no default type is part of the language. The result of a function whose callers decide it ([Rule 8.19](https://typeshade.dev/reference/rules/8-19/)) takes its type from the uses of each call in the same way, an `f32` use included. A `/` over a call of such a function must be refused where some call of the function asks an integer, whichever copy that call takes, so that the meaning of a division does not depend on another line. A `/` or a `/=` whose operands are all integers by inference, such a `let`, variable, field or call among them, must be refused as the `/` over a constant below is. The counter of a counted `for` keeps the type [Rule 7.5](https://typeshade.dev/reference/rules/7-5/) gives it, and the counter of a `for` that is not counted is such a `let`. A `/` over the counter of a `for` that is not counted, where no use gives that counter a type, must be refused as the `/` over a constant below is: the counter was an `i32` before change 0134 (the owner’s decision of 2026-11-11 in the record of change 0134). The default type of a counted `for`’s counter must not decide the type of such a `let` that its start or its bound reads: it asks an integer of no kind, so the `let`’s other uses decide it. A function that writes no result type and returns a value over a function-local constant must return the type that value had before change 0134, where the constant took the integer type of its declared uses. An unannotated `const` whose initializer is written numbers, and a unary minus, `+`, `-`, `*`, `/` and `%` over written numbers and such constants, must be a constant of no fixed type: at module scope, in a function, or as a static field that no code writes. Its value is the ECMAScript value of the initializer. Each read of it must take the type that the written number would take in the same place, and a conversion (`u32(K)`) must convert the written number. A read that takes `f32` names the emitted `f32` declaration, and a read of another type writes the value. A value that comes from such a constant must carry that origin through every expression and binding: an operator, a builtin (`Math.*` and `select` included), `as`, `!`, a conditional, a member, a derived constant, a `let` or a pattern binding (one that a closure or a local function captures included), the start of a loop counter, and the result of a function, a method, a getter or a closure that writes no result type and returns such a value; it loses the origin only where it passes a declared type, an annotation, a declared parameter, field or result, a conversion, or a typed constructor (`vec3u(…)`, `vec2<u32>(…)`). A generic parameter declares no type. A variable initialized by written numbers alone declares no type either, so `(k + w) / 2` with `let w = 3` carries the origin of `k`. A `/` or `/=` whose operands have no declared type, one of which carries the origin of a constant that some read makes an integer (a conversion to `i32` or `u32` and a read in a file that imports it included), must be refused, unless a float-written literal or a float-written constant takes part in an operand, which makes a float division. The quotient’s own read is not another read (Amendment A4 of change 0134): `let j: u32 = K / 2` is `4u` with `const K = 8`, as the written `8 / 2` is. A loop counter whose start carries such an origin must keep the type it had before change 0134, as a counter bounded by one does: the type of the constant’s emitted declaration, or, for a function-local constant, the integer type its declared uses give it. A start through a `let` or a function result takes the type this rule gives that value, the `f32` sentence included, until items 1 and 3 of change 0134 are implemented. A `/` over a tree of written integers whose quotient is no integer (`(1 | 2) / 2`) must be refused as the `/` over a constant is (a module constant whose root is `|`, `&` or `^` over written integers is one tree, and so is the result of a function with no written result that returns one, or of a generic function whose result is its argument), except where a position that states an integer type reads it, which truncates it toward zero (`let j: i32 = (5 | 0) / 2` is 2), and where a comparison beside a peer that states no type or an `f32` reads it, which reads the exact value (a comparison beside a typed `i32` or `u32` is refused with a verified edit, the owner’s decision of 2026-11-12); a variable whose type its uses decide states no type. In such a position the quotient over operands of one integer type is the integer division of that type, never computed through an `f32` (a quotient of an inferred `i32` and an inferred `u32` operand is refused where `u32()` or `i32()` reads it, and the edit named for a read through a binding or arithmetic is an open deviation, [#1467](https://github.com/typeshade/typeshade/issues/1467)), and `i32()` and `u32()` convert it as ToInt32 and ToUint32 do. Only `i32()` and `u32()` convert it across integer types, so a declaration, a parameter or a result of the other integer type stays refused with its verified edit (`let j: u32 = a / 2` over `i32` operands). A divisor that is zero at run time gives TypeScript’s value there, 0, and not WGSL’s `x / 0 = x` (the owner’s decisions of 2026-11-17, Amendment A8 of change 0134). A tree whose root bit operator reads a written number only a `u32` holds is that `u32`’s bits (`3000000000 | 0` is 3000000000): beside a typed `u32`, `+`, `-`, `*`, `&`, `|`, `^`, `<<` and `~` read those bits, which agree with TypeScript’s value modulo 2^32, and every other operation whose result differs between the bits and TypeScript’s value (`/`, `%`, a comparison, `max`, `min`, `clamp`, `abs`, `sign`, `f32()`, `>>` by an amount that is no written number, `/=`, `%=` and `>>=`, and an exported function’s result), also through a binding, `?:`, `select`, a function’s result, a generic function’s result, a module `let`, arithmetic over a written number, a compound assignment and a plain assignment to a variable with no written type, must be refused with a verified edit, `u32(…)` or, where the value flows to an `i32` or into `f32()`, `i32(…)`, and the declaration with its type for an assignment’s target or a constant of no fixed type (none for a constant of another file, and none where no form compiles in a module with this one refusal); an annotation ends the origin (the owner’s decisions of 2026-11-12, “이대로” and D2). This refusal holds over the truncation above. A `/` over such a tree’s value, also through a binding, is refused with it where a `u32` result or annotation, `u32()` or `i32()` reads the quotient (`let d = c; return d / 7;` with `let c = (3000000000|0)` and a `u32` result). An `i32` result or annotation, `f32()` and an operand of another operator keep the refusal of a `/` whose operands are integers by inference (the owner’s decision of 2026-11-17, Amendment A8). A comparison over such a quotient (`d / 7 > 5`) is refused with the edit `i32(d)`, also beside a peer that states no type or an `f32`. `7 / d < f` is refused with no edit. The edit for `d / 7 + vi` is `u32(i32(d) / 7)`. A refusal whose quotient reaches `u32()` or an index through arithmetic (`u32(d / 7 / 1)`, `xs[d / 1000000000]`) names no edit (Amendment A8). A position that states an integer type refuses a `/` over a signed `i32` tree whose two readings differ, with the edit `i32(…)` (Amendment A8). With `let a = 1 << 30; let b = a + a;`, a division, a remainder, a comparison or an equality between `b` and a typed `i32` is refused too: `b / ii`, `b % ii` and `b < ii`. A comparison of such a quotient beside a typed `i32` is refused (`b / 7 > ii`). Beside a peer that states no type or an `f32`, the comparison is refused too. `b / 7 > 5` is refused with the edit `i32(b)`. `b / 7 > f` is refused with no edit (Amendment A8). A typed `i32` position that such a tree reaches (`let b: i32 = a + a`, an argument of a parameter `x: i32`) still reads its wrapped value, an open deviation, [#1380](https://github.com/typeshade/typeshade/issues/1380). The edit that `b /= 7` names, `let b: i32 = a + a`, gives -306783378, where TypeScript gives 306783378. A call `div(b, ii)` of a function whose parameters are typed `i32` compiles to the wrapped quotient. The result of `+`, `-`, `*`, `&`, `|`, `^`, `<<` and `~` over such a tree beside a typed `u32` keeps the tree’s two readings, so a later operation of the list above whose result differs between them is refused; a mask of low bits makes the two agree while the masked value stays at most 2^31 - 1 (`f32((a + vi) & 0xFF)` is 7). An integer that comes out of a tree, a bit operator over operands with no written type, starts a signed `i32` tree: its wrapped `i32` bits and its TypeScript value are its two readings through a binding, an accumulator, `+`, `-`, `*`, `<<` and unary minus. A bit operator and `| 0` over it agree, and an operation of the list above refuses it with the edit `i32(…)` unless a constant fold or a constant trip count proves its range, where the variable’s stores, its initializer included, run at most 24 times in all. A bit operator over an operand of a written type (`vi & 1`) gives that type and starts no tree, and a computed number past `i32` that reaches a bit operator through a variable starts a `u32` tree. A tree whose two readings differ is refused beside a typed `i32` operand too, and an integer typed only by its peer (`let s = 0; s += ii`) keeps the arithmetic of its WGSL type ([Rule 1.3](https://typeshade.dev/reference/rules/1-3/)) (the owner’s decisions of 2026-11-16 to 2026-11-18, Amendments A7 and A8). A compound assignment is no position that states a type for its right side, so a quotient there is refused as in the written-out form. An exported constant whose own file refuses its quotient keeps the `f32` its importers read, as an exported function’s result does (Amendment A5); any other exported constant is read as it is, and an importer names no edit that lands in another file. Only a type the author wrote counts as declared (an annotation, a written result type, a conversion, also one inside a builtin’s argument, a typed parameter, field or element): any other integer, a call’s result of any kind, a generic result, a module `let` and a read of a variable assigned anywhere included, is an integer by inference for the `/` above and carries the two readings of every value stored in it (the owner’s decision of 2026-11-13, E1). A field whose initializer is a written conversion or value-type constructor (`v = u32(0)`, `p = vec2u(0, 0)`) has a written type, and a field initialized by a written number (`v = 0`) has none (the owner’s decision of 2026-11-17, Amendment A7 of change 0134). A binding or a field with no written type is no typed peer of a tree, whatever it holds: with `let a = (0xF0000000|0)`, `let b = vi; f32(a + b)` and `s.v = vi; f32(a + s.v)` are refused, and `let b: u32 = vi` is a typed peer. A value the compiler cannot prove must be refused, never accepted: a bound of the walk, a node kind it does not know, a store it cannot read, a builtin other than `abs`, `min`, `max`, `clamp` and `sign` over a tree, a mask over an unproven value, an element of a destructuring pattern that a tree reaches, an unproven value beside a typed peer, and a field with no written type that is read beside a tree or in a position the two readings decide (a type parameter names no class, so its fields are read by name). An annotation that the editor projects into an open import is no written type. The refusal says `The compiler cannot prove that the u32 bits and the TypeScript value of "a + b" agree, so the "f32()" does not say which value it means. Write "i32(a + b)" for the i32 value.` (the owner’s decisions of 2026-11-16 and 2026-11-17, Amendment A7). A refusal states the two readings as numbers only where no variable it involves is reassigned after its declaration; every other refusal says this sentence with its verified edit (the owner’s decision of 2026-11-18, Amendment A8). A field with no written type in an expression that no tree reaches is read as before: `s.v = 5; f32(s.v / 2)` is 2.5. A tree of written integers in a position that takes an `f32` is refused, and the refusal names the tree with the verified edit `f32(…)` around it (E3). Every edit a refusal names, a `>>`’s and a value form’s included, is named only where it compiles with the module’s other edits and the edited program gives TypeScript’s value (an edit under `abs` over a nested conversion still gives WGSL’s value, an open deviation, [#1388](https://github.com/typeshade/typeshade/issues/1388)). A builtin whose edit cannot be verified names no edit, and `abs` over an `i32` that the compiler proves to be the `i32` minimum is refused with no edit; `abs` of an `i32` known only at run time keeps WGSL’s rule (the owner’s decisions of 2026-11-18, Amendment A8). A refusal that names no edit offers no wrap where the structure shows that the wrap would not choose the division (an `f32(…)` wrap read by a bit operator, an integer wrap of a divisor whose integer quotient is 0, a `u32(…)` wrap read by a unary minus, a constant’s sentence over a division that holds a tree), and says what is true instead (the owner’s decision of 2026-11-13). A conversion the author writes on an operand, also inside its arithmetic (`(1 | 2) / (i32(2) + 1)`), states its type, and the `/` is that type’s division; a builtin carries its arguments’ origin. The refusal must name one edit, whose type the structure of the quotient’s use decides: the constant declared with the type an integer position asks, the `/` with each such value converted, or, for a `/=`, the variable declared with its type (`let y: f32 = k`). An exported function’s result, in every export form, is the `f32` the host reads, and decides a quotient that flows there. The edits of every refused `/` of the module must be made at once and the program compiled, and the edits named only where that adds no error; one more set may be compiled, and no more than two a module. Where they do not compile together, no refusal of the module names an edit: each names the operands to wrap and the types its positions take, or the variable to declare with a type (the owner’s decision of 2026-11-12, Amendment A6 of change 0134). A mismatch around the `/` is the refusal said again, so the mistake is one diagnostic ([Rule 12.4](https://typeshade.dev/reference/rules/12-4/)).

The rule text and the parts under it are the compiler's own English, as the design document writes them.

## Rationale

this is WGSL’s abstract-integer rule, read from every place a number reaches, so that an author writes a number as a number (`docs/owner-decisions.md` OD-7). The `f32` where nothing decides is the type such a value always had on this surface, so no program that compiled computes another value.

## Derives from

[Abstract Numeric Types](https://gpuweb.github.io/gpuweb/wgsl/#abstract-types) and [Conversion Rank](https://gpuweb.github.io/gpuweb/wgsl/#conversion-rank); surface §13.

## How it is verified

Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.

Where the rule says the compiler enforces it:

`src/compiler/ts/lit-coerce.ts`, `src/compiler/ts/local-numeric-inference.ts`, `src/compiler/ts/local-numeric-inference.test.ts`, `src/compiler/ts/int-lit-context.test.ts`, and `int-lit-coerce.test.ts`; for a value its uses decide, `src/compiler/ts/use-inference.ts` and `src/compiler/ts/use-inference.test.ts` (each place in both halves with the hover, the chain and its source order, the refused `/` and `/=`, the counter of an open `for` and the `/` over it, and a counted `for` over such a `let`), and `src/compiler/ts/use-inference-module.test.ts` (a module variable, a static field, a class field and a function’s result its callers decide, with its copies, in both halves with the hover); for a list of integer-written numbers, `src/compiler/ts/lower/expression-array/list-uses.ts` and `src/compiler/ts/list-by-uses.test.ts` (each use in both halves with the hover, the conflicts, the `f32` default and the refused `/`, with the CPU oracle); for a constant of no fixed type, `src/compiler/ts/abstract-const.ts` and `src/compiler/ts/abstract-const.test.ts` (each read in both halves, the exact value past 2^24 on the CPU oracle and in the WGSL and GLSL text, and the refused `/`).

The files that verify it at commit af54ddad5, each at the first line that names the rule:

- [`src/compiler/ts/abstract-const.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/abstract-const.test.ts#L1)
- [`src/compiler/ts/abstract-const.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/abstract-const.ts#L1)
- [`src/compiler/ts/int-lit-coerce.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/int-lit-coerce.test.ts#L1)
- [`src/compiler/ts/int-lit-context.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/int-lit-context.test.ts#L1)
- [`src/compiler/ts/int-lit-range.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/int-lit-range.test.ts#L1)
- [`src/compiler/ts/list-by-uses.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/list-by-uses.test.ts#L1)
- [`src/compiler/ts/lit-coerce.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lit-coerce.ts#L1)
- [`src/compiler/ts/local-numeric-inference.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/local-numeric-inference.test.ts#L1)
- [`src/compiler/ts/local-numeric-inference.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/local-numeric-inference.ts#L1)
- [`src/compiler/ts/lower/expression-array/list-uses.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/expression-array/list-uses.ts#L1)
- [`src/compiler/ts/module-let-list.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/module-let-list.test.ts#L1)
- [`src/compiler/ts/use-inference-module.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/use-inference-module.test.ts#L1)
- [`src/compiler/ts/use-inference.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/use-inference.test.ts#L1)
- [`src/compiler/ts/use-inference.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/use-inference.ts#L1)

## Explained in

The sections of the surface document that explain this rule, at commit af54ddad5:

- [§13 Integer literals](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#13-integer-literals)
- [§18 A list as an array's initializer](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#18-a-list-as-an-arrays-initializer)
- [§24 Module variables](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#24-module-variables)
- [§26 Classes with methods, a constructor and static functions](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#26-classes-with-methods-a-constructor-and-static-functions)
- [§63 An array's methods](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#63-an-arrays-methods)

## See also

- [Rule 4.8](https://typeshade.dev/reference/rules/4-8/) Test
- [Rule 5.2](https://typeshade.dev/reference/rules/5-2/) Test
- [Rule 1.3](https://typeshade.dev/reference/rules/1-3/)
- [Rule 7.5](https://typeshade.dev/reference/rules/7-5/)
- [Rule 8.13](https://typeshade.dev/reference/rules/8-13/)
- [Rule 8.14](https://typeshade.dev/reference/rules/8-14/)
- [Rule 8.19](https://typeshade.dev/reference/rules/8-19/)
- [Rule 12.4](https://typeshade.dev/reference/rules/12-4/)
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

- [`docs/language-design.md:422`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L422) (the design document)
- [`reqs/rules/RULE-0501.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0501.md) (its traceability item)
