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 method in TypeScript is a class member function that is accessible from any context, including internally within the defining class, derived subclasses, and externally via instantiated objects. It represents the least restrictive access level in TypeScript’s encapsulation model. By default, all class methods in TypeScript are implicitly public unless explicitly marked with private or protected.
class DataProcessor {
    // Explicitly declared public method
    public process(): void {
        // Implementation
    }

    // Implicitly public method (default behavior)
    initialize(): void {
        // Implementation
    }
}

Technical Characteristics

Visibility and Access Public methods expose the API surface of a class. They can be invoked directly on an instance of the class using dot notation.
const processor = new DataProcessor();
processor.process(); // Valid external access
Inheritance When a class is extended, public methods are inherited by the subclass. They retain their public visibility in the derived class and can be accessed via the this context, the super keyword, or externally on the subclass instance.
class BaseNode {
    public compute(): number {
        return 42;
    }
}

class ChildNode extends BaseNode {
    public delegateComputation(): number {
        // Accessible internally within the subclass
        return super.compute(); 
    }
}

const node = new ChildNode();
node.compute(); // Accessible externally on the subclass instance
Interface Implementation When a class implements a TypeScript interface, the methods fulfilling the interface contract must be public. TypeScript enforces this because interfaces define the public-facing shape of an object; they cannot dictate private or protected members.
interface Executable {
    execute(): boolean;
}

class Task implements Executable {
    // Must be public to satisfy the Executable interface
    public execute(): boolean {
        return true;
    }
}
Compilation Behavior The public keyword is strictly a TypeScript compile-time construct used for static type checking. During transpilation to JavaScript, the TypeScript compiler strips the public modifier entirely. The resulting JavaScript output defines standard prototype methods, meaning the runtime behavior is identical regardless of whether the public keyword was explicitly written or implicitly assumed.
Master TypeScript with Deep Grasping Methodology!Learn More