---
title: "Rule 5.4: Literals and typing, a TypeShade design rule"
description: "Rule 5.4 of the TypeShade language design, in Literals and typing. A literal must fit the type it takes…"
url: https://typeshade.dev/reference/rules/5-4/
language: en
---

# Rule 5.4

Chapter 5, Literals and typing Test

A literal must fit the type it takes; `-1` in a `u32` position and `2147483648` in an `i32` position are refused as written.

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

## Rationale

an out-of-range literal has no value in the type and would be a different number on the GPU.

## Derives from

[Literals](https://gpuweb.github.io/gpuweb/wgsl/#literals) (an integer literal must be representable in its type).

## 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:

[`TS8003`](https://typeshade.dev/reference/errors/ts8003/) (surface §13, “the value has to fit”).

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

- [`src/compiler/ts/field-initializer-range.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/field-initializer-range.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/local-numeric-inference.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/local-numeric-inference.test.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)

## Error codes that enforce it

The diagnostic codes the rule names under Enforced by, or whose registry text names the rule:

**[`TS8003`](https://typeshade.dev/reference/errors/ts8003/) `TYPE_MISMATCH`**

Two types that do not fit where they meet (an operator, a condition, a declaration, a return or an argument), a read of an optional value (`T | undefined`, `T | null` or `T | null | undefined`) that no test narrowed ([Rule 7.10](https://typeshade.dev/reference/rules/7-10/)), or a constant the compiler folds that WGSL refuses: a shift amount past 31, a divisor of 0, clamp bounds that cross, an `f32` past its range or a builtin call outside its domain, such as `Math.pow(-2., 2.)`.

## See also

- [Rule 5.3](https://typeshade.dev/reference/rules/5-3/) Test
- [Rule 5.5](https://typeshade.dev/reference/rules/5-5/) Test
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

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