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 a write to that place when the initializer is a place: a name, a field or an element of one, or a call of a function of the file whose every return names one parameter (Rule 8.8, rule 1). A const initialized from a place is a second name of that place, and nothing is emitted for it (Rule 8.8, change 0142). A const initialized from any other call that may give an object that has a name is refused with the remedy, as TS8099, where the function writes an object after the declaration (Rule 8.8, rule 4), unless the program never writes an object of that type (Rule 8.8, rule 6). A const nothing writes into stays WGSL’s let. v, or a field or an element of v, is such a write when it is handed to a parameter that its function writes (Rule 8.25).

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 initialized from a field or an element holds an object something else holds: TypeScript’s write reaches the object both hold, so the const is a second name of that place and the write reaches the place (change 0142, Amendment A3, rule 1). A const initialized from a call that may give an object that has a name cannot be that place, and a copy would not show a write, so the front end refuses the declaration where the function writes an object after it, as not supported yet (change 0142, stage 2), and names the remedy. Before this rule every write through a const was refused, with let as the remedy. An object has the identity a JavaScript object has (Rule 8.8), so const w = v and let w = v make w a second name of v, always and not only where a write follows; Rule 7.2’s table records both.

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:

TS8099 for a const that takes a call’s result where the function writes an object after it ("c" would name an object that "pick(a, b)" gives back, which may already have a name, and the function writes an object after that. One object under two names is not supported yet (change 0142, stage 2). Read what you need from the call first, or write through the name that holds the object., src/compiler/ts/lower/second-name.ts), which also says a write through that const once, so that the write adds no second diagnostic; TS8005 CONST_ASSIGN for a write to a component of a const vector or matrix, which is a value ("v" is a const, and a write to a component of it changes it. Declare it with let to write it.), TS8035 CLASS_MEMBER for a method that writes its object called on a const that holds a copy; 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, and by src/compiler/ts/object-reference.test.ts and src/compiler/ts/second-name.test.ts for the second name; examples/gallery/class-parts/class-parts.shade.ts in the compile gate.

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

Explained in

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

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 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).
TS8035 CLASS_MEMBER
A class member shape that the surface does not take, a use of a member that the class rules refuse, or a new that builds no class (Rule 8.13). Getters and setters, static fields and methods, overload signatures, abstract members, # private names and methods that change their object all compile. The message names the member or the target and the edit.
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

Source

The rule at commit af54ddad5:

Edit this page Report a problem