Rule 6.10
On this page

Rule 6.10

Chapter 6, Declarations and resources Test

A local const binds its name once and leaves what it holds as writable as TypeScript does. A write into it (to a field, a component or an element, or by a method that writes its object) must change the value when the initializer builds one nothing else holds (new, an object literal, an array literal or a type’s constructor, alone or with methods called on it), the declaration being emitted as a var from then on; it must be refused with the remedy when the initializer may be a value something else holds (a name, a field, an element, a parameter, a function’s result). A const nothing writes into stays WGSL’s let.

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

Rationale

const v = new V(); v.bump() is how TypeScript is ordinarily written, since a const fixes the name and not the object; nothing else holds the object new built, so the local is that object, and a write through it changes what TypeScript changes. A const that copies a value another name holds is where the two would part: TypeScript’s write reaches the object both names hold, and a write here would reach the copy alone, so the front end asks which is meant, a let to write a copy or the write on the value itself. Before this rule every write through a const was refused, with let as the remedy. A struct is a value here, so const w = v and let w = v copy it, where TypeScript hands w the object v holds; a write through either name after the copy is not seen through the other, which Rule 7.2’s table records.

Derives from

ECMAScript let and const declarations (a const binding cannot be assigned again, and what it holds is not frozen); Value Declarations and var Declarations; Rule 8.10; surface §26.

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:

TS8005 CONST_ASSIGN for a write through a const that may hold a shared value ("v" is a const whose value may be one something else holds, which TypeScript would change with it and a copy here would not. Declare it with let to write a copy, or write through the value itself.), TS8035 CLASS_MEMBER for a method that writes its object called on one; pinned by src/compiler/ts/member-assign.test.ts, src/compiler/ts/class-methods.test.ts and src/compiler/ts/class-syntax.test.ts, which also holds the three CPU paths to one value for a write through a const that built its value; examples/class-parts.shade.ts in the compile gate.

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

Explained in

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

Error codes that enforce it

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

TS8005 CONST_ASSIGN
An assignment to a name that cannot change, such as a const or a read-only resource.
TS8035 CLASS_MEMBER
A class member shape the surface does not take, or a use of a member the class rules refuse, and a new that builds no class (Rule 8.13).

See also

Source

The rule at commit 26de7be8:

Edit this page Report a problem