Rule 8.16
On this page

Rule 8.16

Chapter 8, Functions and entry points Test

An instance field that holds an arrow function or a function expression is a method of its class under the field’s name: the function’s parameters, return type and body are the method’s, an expression body is the value it returns, and this in it is the object, as it is in TypeScript. With no return type written, it returns what its body does, and an expression body that is an assignment, ++ or -- runs as a statement (Rule 8.19). A static field that holds a function, a function with type parameters, and an async or generator function must be refused with the remedy. A member a class that extends declares again must keep the kind the class above gives it, as TypeScript requires (a field, a method or an accessor, a field that holds a function standing where a method was and a field where an abstract accessor was), and super.f on a field that holds a function must be refused; so must an accessor over an abstract field, which TypeScript takes and which a struct here cannot mean.

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

Rationale

onHit = (d: f32): f32 => d * this.k is how TypeScript code often writes a method, to keep this bound; a shader has no function value to hand anywhere, so the one thing such a field can be is the method it is written as, called as o.onHit(d), and this is the object in both. A static one binds this to the class that declares it where a static method binds it to the class the call names (Rule 8.13), so it is written as the static method it means; type parameters need a generic function. TypeScript refuses a member that changes kind in a class that extends, and without the check each such program compiled to the derived class’s member where TypeScript’s object holds the base’s; a field is the object’s own, so super does not reach one. An abstract field is laid out in every struct below the class that declares it, so a body that class wrote reads the member and never an accessor a class below declares, 0 where TypeScript computes the getter; abstract get f(): T says the same thing and reaches the accessor.

Derives from

ECMAScript class field definitions and arrow functions (this is the object the field initializer runs for); TypeScript’s override checks (TS2416, TS2423, TS2425, TS2426, TS2610, TS2611) and TS2855 (super on a field); Rule 8.9; Rule 8.13; 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:

TS8035 CLASS_MEMBER for each refusal (A static field holding a function is a static method: write "static f(...) { ... }".; "B.f" is a method, and the "A.f" it overrides is a field that holds a function; TypeScript refuses an override of another kind. Declare it as a field that holds a function, or rename it.; "super.f" names a field that holds a function, and a field is the object's own, which "super" does not reach. Declare "A.f" as a method, or write "this.f".), pinned by src/compiler/ts/class-syntax.test.ts and src/compiler/ts/class-methods.test.ts, which also hold the three CPU paths to one value for a field that holds a function, an override of one, and a method a field overrides; 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:

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