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 field in TypeScript is a class property that is accessible without restriction from any context, including internally within the defining class, within derived subclasses, and externally via instances of the class. By default, all class members in TypeScript are implicitly public unless explicitly restricted using the private or protected access modifiers, or ECMAScript private fields (#).

Syntax and Declaration

Public fields can be declared implicitly by omitting an access modifier, or explicitly using the public keyword.
class DataNode {
    // Implicitly public field
    nodeId: string;

    // Explicitly public field
    public isActive: boolean = true;

    constructor(id: string) {
        this.nodeId = id;
    }
}

Constructor Parameter Properties

TypeScript provides syntactic sugar for declaring and initializing public fields directly within the constructor signature. By prefixing a constructor parameter with the public modifier, the compiler automatically generates the corresponding public field and assigns the argument to it upon instantiation.
class Config {
    // Automatically declares 'public endpoint: string' and assigns the argument to 'this.endpoint'
    constructor(public endpoint: string) {}
}

Technical Characteristics

  • Type Erasure: The public keyword is a TypeScript-specific access modifier. It exists strictly at compile-time for structural typing and access control. The keyword is completely erased during the emit phase and does not alter the runtime behavior of the resulting JavaScript.
  • Definite Assignment: When the strictPropertyInitialization compiler option is enabled, public fields must be initialized either at their declaration or synchronously within the constructor. If a field is initialized via indirect means (e.g., a dependency injection framework), the definite assignment assertion operator (!) must be appended to bypass the compiler check.
class Service {
    public initializedField: number = 1;
    public deferredField!: string; // Definite assignment assertion
}
  • JavaScript Emission: The compilation of public fields is dictated by the target and useDefineForClassFields flags in tsconfig.json.
    • Legacy Emission: When useDefineForClassFields is false, initialized public fields are emitted as standard property assignments (this.fieldName = value;) injected directly into the constructor function.
    • Standard ECMAScript Emission: When useDefineForClassFields is true and the target is ES2022 or newer, TypeScript emits public fields using standard ECMAScript class field syntax.
    • Polyfilled Standard Emission: When useDefineForClassFields is true but the target is older than ES2022 (e.g., ES2015), TypeScript does not use class field syntax. Instead, it emits Object.defineProperty calls inside the constructor to polyfill the standard ECMAScript semantics.
  • Uninitialized Fields and the declare Modifier: Under standard ECMAScript semantics (useDefineForClassFields: true), uninitialized public fields (e.g., public name: string;) are emitted and initialized to undefined on the instance. This behavior can inadvertently overwrite prototype-level getters or setters. To define a field’s type without emitting any runtime initialization code, use the declare modifier.
class Base {
    get name() { return "BaseNode"; }
}

class Derived extends Base {
    // Prevents emission of `name;` or `Object.defineProperty`, 
    // preserving the prototype getter while providing type information.
    declare public name: string; 
}
Master TypeScript with Deep Grasping Methodology!Learn More