---
title: "Rule 6.2: Declarations and resources in TypeShade"
description: "Rule 6.2 of the TypeShade language design, in Declarations and resources."
url: https://typeshade.dev/reference/rules/6-2/
language: en
---

# Rule 6.2

Chapter 6, Declarations and resources Test

A storage binding’s access mode is inferred from what the program does: a binding that some entry writes, by assignment, a mutating method, an atomic builtin, or a write through a function or a reference parameter, is `var<storage, read_write>`, and every other storage binding is `var<storage, read>`. A write inside a function that no entry reaches does not make the binding `read_write` and draws no [`TS8005`](https://typeshade.dev/reference/errors/ts8005/), and a function that no entry reaches and that writes such a binding is not emitted (change 0114, Amendment A17). A write to a binding that `readonly` or `"read"` promises is refused wherever it stands, because the editor reads that promise from the type. `readonly` in the value type (`storage<readonly T[]>`) is the author’s promise that nothing writes the binding, and a write to it is refused. The compiler still accepts an explicit second type argument: `"read_write"` keeps `var<storage, read_write>` and `"read"` keeps `var<storage, read>`, and the only two words it takes are `"read"` and `"read_write"`, spelled as string literal types. Change 0114, decision D3, removes the `"read_write"` argument in S4; D3 does not name `"read"`. A `uniform` binding is read-only and has no access mode, so `uniform<T>` takes one type argument. A write to a read-only resource must be refused, by the compiler and by the editor. A binding that the module writes and a vertex entry reads is refused, and so is a binding declared `"read_write"` that a vertex entry reads, written or not, and a storage binding that a vertex entry writes, directly or through a function it calls, because WebGPU allows no writable storage buffer in the vertex stage.

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

## Rationale

the compiler reads the access mode from the program, as it reads the address space of a module variable from a top-level `let` (OD-3), and `readonly` is TypeScript’s own word for “not written”; the host’s bind group layout follows the inferred mode, so an edit that adds or removes a write changes the layout; the declaration keyword cannot carry the mode, because a TypeScript `const` array forbids rebinding the name and permits `arr[0] = 1`.

## Derives from

[`var` Declarations](https://gpuweb.github.io/gpuweb/wgsl/#var-decls) and [Memory Access Mode](https://gpuweb.github.io/gpuweb/wgsl/#memory-access-mode); surface §7. The two words are WGSL’s own enumerants and already appear on this surface in `texture_storage_2d<"r32float", "read_write">`, so neither takes a §9.3 row.

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

- [`TS8002`](https://typeshade.dev/reference/errors/ts8002/) for an access word outside the two, and for a second type argument on a `uniform<T>`, raised in `src/compiler/ts/bindings.ts`, which recovers the binding as `read_write` so that the author’s own write is not a second sentence on the same program; the inference is `src/compiler/ts/storage-access.ts`, which also refuses a written binding that a vertex entry reads, a binding declared `"read_write"` that a vertex entry reads, and a storage binding that a vertex entry writes, each with [`TS8099`](https://typeshade.dev/reference/errors/ts8099/), and which drops a function that no entry reaches and that writes a binding the program leaves `read`, so that the write draws no [`TS8005`](https://typeshade.dev/reference/errors/ts8005/);
- `TS8099` for the retired `{ access }` option of the call form, raised in the same file, whose sentence names the type-argument spelling to write;
- `TS8005 CONST_ASSIGN` for a write to a read-only resource, whose sentence names the `storage<T>` remedy for a write to a binding whose value type is `readonly` (`src/compiler/ts/lower/readonly-promise.ts`) and the `storage<T, "read_write">` remedy for every other storage binding the write is refused on (`writableRemedy` in `src/compiler/ts/context.ts`; the `"read_write"` spelling in that remedy is one that change 0114, decision D3, removes in S4), and for an atomic builtin on a read binding, whose sentence names the mode rather than the keyword (`src/compiler/ts/lower/atomics.ts`). It names no line in the two cases where no line would close the program: when the binding’s declared value type was itself refused and recovered, since the line would be built from a type the compiler could not read (`storage<mat2x3<f64>>` was answered with `storage<mat2x3, "read_write">`, the `<f64>` dropped), and when the target is no place on either mode, which is why the root’s access is checked after the target is known to be one, for an element (`md[0]` on an `f64` matrix) and for a member after every one of that branch’s refusals (`src.length`, an emulated-double lane `dv[0].x`, a multi-component swizzle `v.xy`);
- TypeScript’s own codes in the editor, because the ambient library resolves `uniform<T>` and `storage<T, "read">` to `ReadView<T>` and `storage<T>` and `storage<T, "read_write">` to `T` itself, so a `readonly` value type draws the error from its own type: `TS2542` on an indexed write (`src[0] = 1.`), `TS2540` on a field write (`u.scale = 1.`), `TS2344` on an access word outside the two and `TS2314` on a second type argument to a `uniform` (`src/language-service/ambient-storage-access.test.ts`);
- `src/compiler/ts/declare-bind.test.ts` for the access mode each spelling collects, and `src/compiler/ts/remedy-lines.test.ts` for the lines these sentences name: every refusal that quotes a declaration has that declaration written back into its own program, which must then compile clean and be clean in the editor;
- not by the editor in two places, which Appendix B records: an atomic builtin on a read binding and a mutating method on a class-typed read binding are refused by the compiler alone.

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

- [`src/compiler/ts/bindings.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/bindings.ts#L1)
- [`src/compiler/ts/context.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/context.ts#L1)
- [`src/compiler/ts/declare-bind.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/declare-bind.test.ts#L1)
- [`src/compiler/ts/lower/atomics.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/atomics.ts#L1)
- [`src/compiler/ts/lower/readonly-promise.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/readonly-promise.ts#L1)
- [`src/compiler/ts/member-assign-emit.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/member-assign-emit.test.ts#L1)
- [`src/compiler/ts/member-assign-places.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/member-assign-places.test.ts#L1)
- [`src/compiler/ts/member-assign.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/member-assign.test.ts#L1)
- [`src/compiler/ts/remedy-lines.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/remedy-lines.test.ts#L1)
- [`src/compiler/ts/storage-access-paths.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/storage-access-paths.test.ts#L1)
- [`src/compiler/ts/storage-access.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/storage-access.test.ts#L1)
- [`src/compiler/ts/storage-access.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/storage-access.ts#L1)
- [`src/language-service/ambient-matrix-index.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/language-service/ambient-matrix-index.test.ts#L1)
- [`src/language-service/ambient-storage-access.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/language-service/ambient-storage-access.test.ts#L1)
- [`src/language-service/ambient-texture-lowering.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/language-service/ambient-texture-lowering.test.ts#L1)
- [`src/language-service/ambient-texture-refusals.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/language-service/ambient-texture-refusals.test.ts#L1)
- [`src/language-service/ambient-vector-math.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/language-service/ambient-vector-math.test.ts#L1)
- [`src/language-service/ambient.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/language-service/ambient.test.ts#L1)

## Explained in

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

- [§1 Resources — `declare`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#1-resources--declare)
- [§7 Diagnostics (required)](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#7-diagnostics-required)
- [§9 Assignment targets](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#9-assignment-targets)
- [§23 Atomics](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#23-atomics)

## Error codes that enforce it

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

**[`TS8002`](https://typeshade.dev/reference/errors/ts8002/) `UNKNOWN_TYPE`**

A type name that nothing declares, in a parameter, a type argument, an alias, an `implements` clause or a base class, or a type that this surface does not take where the mapper reads one ([Rule 2.1](https://typeshade.dev/reference/rules/2-1/)). A base that the file declares and does not collect as a struct is `STRUCT_FIELD`.

**[`TS8005`](https://typeshade.dev/reference/errors/ts8005/) `CONST_ASSIGN`**

An assignment to a name or a place that cannot change: a `const` number or vector, a component of a `const` vector or matrix, a `readonly` field or static, or a storage binding that `readonly` or `"read"` promises is never written. A write to a field or an element of a `const` object is not one of these, because the `const` holds the object and the object stays writable, as in JavaScript ([Rule 6.10](https://typeshade.dev/reference/rules/6-10/)).

**[`TS8099`](https://typeshade.dev/reference/errors/ts8099/) `UNSUPPORTED`**

The catch-all for a diagnostic whose site does not yet deserve its own code. It stays parked at TS8099, past the sequential range.

## See also

- [Rule 6.1](https://typeshade.dev/reference/rules/6-1/) Test
- [Rule 6.3](https://typeshade.dev/reference/rules/6-3/) 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:482`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L482) (the design document)
- [`reqs/rules/RULE-0602.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0602.md) (its traceability item)
