Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A public setter in TypeScript is an accessor method defined with the set keyword and the public access modifier (either explicit or implicit) that binds an object property to a function. It intercepts assignment operations to a specific property, executing custom logic before mutating the underlying internal state of the class instance. Because it is public, the setter can be invoked from anywhere the class instance is accessible.

Syntax

class ClassName {
    private _propertyName: Type;

    public set propertyName(value: Type) {
        this._propertyName = value;
    }
}

Compiler Rules and Constraints

When defining a public setter in TypeScript, the compiler enforces strict structural rules:
  1. Single Parameter: A setter must take exactly one parameter. Defining a setter with zero parameters or multiple parameters will result in a compilation error (TS1049).
  2. No Return Type: A setter cannot have a return type annotation. The TypeScript compiler implicitly understands that setters do not return values. Attempting to type the return (e.g., set prop(val: string): void) throws error TS1095.
  3. Target Environment: Accessors require the TypeScript compiler to target ECMAScript 5 or higher. Compiling to ES3 will result in an error.
  4. Implicit Public Visibility: In TypeScript, class members are public by default. Omitting the public keyword still results in a public setter, though explicitly declaring it is standard practice for strict access control visibility.

Type Relationships (TypeScript 4.3+)

Historically, TypeScript required the parameter type of a setter to exactly match the return type of its corresponding getter. As of TypeScript 4.3, setters can accept a wider type than the getter returns, provided the getter’s return type is assignable to the setter’s parameter type.
class TypeContainer {
    private _internalValue: string = "default";

    // Getter returns a strict type
    public get value(): string {
        return this._internalValue;
    }

    // Public setter accepts a wider union type
    public set value(val: string | number | boolean) {
        this._internalValue = String(val);
    }
}

const container = new TypeContainer();

// Valid assignments invoking the public setter
container.value = 100;       
container.value = true;      
container.value = "string";  

// Type evaluation
const retrieved: string = container.value; 

Execution Mechanics

Under the hood, TypeScript compiles public setters into standard JavaScript Object.defineProperty calls (or ES6 class setter syntax, depending on the compilation target). The setter is not invoked as a standard method instance.propertyName(value), but rather through standard assignment syntax instance.propertyName = value.
Master TypeScript with Deep Grasping Methodology!Learn More