- Sound mode S-08, pre-alpha
- Design record 0007, Accepted
TSR0802
'this' escapes before '{0}' is initialised. Assign every field first.
'this' is passed out, used to call a method, or captured before every field is assigned. Assign every field first.
Rule S-08: Fields are initialised before `this` escapes (level: sound). Reported by ts-r-check and ts-r-lsp in files that opt in to sound mode. See Sound mode.
What it means
Until every field is definitely assigned, a constructor or field initialiser may only use this to assign a field or read one already assigned. Calling a method or accessor on this, passing this as an argument, storing or returning it, or creating an arrow function that mentions it lets other code see an object with missing fields. Optional fields and fields whose type includes undefined count as assigned from the start.
Example
// Registering this before its id is set.
const registry = new Set<object>();
class Entity {
id: string;
constructor(id: string) {
registry.add(this); // @expect TSR0802
this.id = id;
}
}
new Entity("a");
export {};
How to fix
- Assign every field before the first use of
thisother than field assignments. - Initialise fields in their declarations where possible.
Sound mode is defined in design record 0007 (accepted 2026-10-15).