Access Modifiers in TypeScript Last Updated : 23 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In TypeScript, access modifiers control the visibility and accessibility of class members, such as properties and methods, aligning with the principles of encapsulation and information hiding in object-oriented programming.Public: Members are accessible from anywhere; this is the default modifier if none is specified.Private: Members are accessible only within the class they are defined in.Protected: Members are accessible within the class they are defined in and in subclasses. JavaScript class Animal { public name: string; private age: number; protected species: string; constructor(name: string, age: number, species: string) { this.name = name; this.age = age; this.species = species; } public getInfo(): string { return `${this.name} is a ${this.species}.`; } } class Dog extends Animal { constructor(name: string, age: number) { super(name, age, 'Dog'); } public getDetails(): string { return `${this.name} is a ${this.species} and is ${this.age} years old.`; } } const myDog = new Dog('Buddy', 3); console.log(myDog.name); // Accessible console.log(myDog.getInfo()); // Accessible console.log(myDog.getDetails()); // Accessible name is public: accessible from anywhere.age is private: accessible only within the Animal class.species is protected: accessible within Animal and its subclass Dog.Output:BuddyBuddy is a Dog.Buddy is a Dog and is 3 years old.Types of Access Modifiers1) Public Access ModifierThe public modifier allows class members to be accessible from anywhere. By default, all class members are public if no access modifier is specified. JavaScript class Animal { public name: string; constructor(name: string) { this.name = name; } public makeSound(): void { console.log(`${this.name} makes a sound.`); } } const dog = new Animal('Dog'); console.log(dog.name); // Accessible dog.makeSound(); // Accessible name and makeSound are public, allowing access from outside the class.We can create an instance of Animal and access its name property and makeSound method directly.Output:DogDog makes a sound.2. Private Access ModifierThe private modifier restricts access to class members, making them accessible only within the class they are defined. This ensures encapsulation and protects the internal state of the object. JavaScript class Person { private ssn: string; constructor(ssn: string) { this.ssn = ssn; } public getSSN(): string { return this.ssn; } } const person = new Person('123-45-6789'); console.log(person.getSSN()); // console.log(person.ssn); ssn is private, preventing direct access from outside the class.The public method getSSN provides controlled access to the private ssn property.Output:123-45-67893) Protected Access ModifierThe protected keyword is used to declare a class member so that it can be accessed by the class containing it and any of its subclasses, it comes handy when you want members of a class accessed in descendant classes but not outside. JavaScript class User { protected age: number; constructor(age: number) { this.age = age; } } class Employee extends User { public getRetirementAge(): number { return this.age + 65; } } const employee = new Employee(30); console.log(employee.getRetirementAge()); //console.log(employee.age); age is protected, allowing access within User and its subclass Employee.Attempting to access age directly from an instance of Employee results in an error.Output:95Best Practice of Using Access Modifiers in TypeScriptExplicitly Define Access Modifiers: Always specify public, private, or protected for class members to enhance code clarity and maintainability. Encapsulate Class Members: Use private or protected to restrict access to class properties and methods, ensuring internal implementation details are hidden. Start with the Least Visible Modifier: Begin with private for class members and increase visibility only as necessary to maintain encapsulation. Comment More infoAdvertise with us Next Article Access Modifiers in TypeScript pankajbind Follow Improve Article Tags : Web Technologies TypeScript TypeScript-Questions Similar Reads TypeScript Accessor TypeScript accessors, through get and set methods, offer controlled access to object properties, enhancing encapsulation, validation, and custom logic. By using accessors, you can manage how properties are accessed and modified, supporting a more robust object-oriented design in your classes.Getters 3 min read TypeScript Indexed Access Types TypeScript's Indexed Access Types allow you to access the type of a specific property within an object or array using bracket notation (Type[Key]). This feature provides precise type information about properties or elements, enhancing type safety when accessing deeply nested structures or specific o 3 min read TypeScript - Access Modifiers and Readonly Properties In TypeScript, access modifiers and readonly properties help define how class members (properties and methods) can be accessed and modified. Access modifiers control the visibility of a class member, determining where it can be accessed from. Readonly properties, on the other hand, ensure that a var 5 min read What are the Modules in Typescript ? Modules in TypeScript allow you to organize code into reusable, manageable, and logical units by encapsulating functionalities into separate files.They help avoid global namespace pollution by providing scoped declarations.Modules can be imported and exported, enabling code reuse and better maintain 4 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read Interfaces in TypeScript TypeScript is a statically typed superset of JavaScript that adds optional types, classes, interfaces, and other features to help developers build robust and maintainable applications. One of the most powerful features of TypeScript is interfaces, which allow you to define the structure of objects, 4 min read How to Access TypeScript Private Members ? In this TypeScript article, we will learn how to access the TypeScript private members using different approaches. We can interact with the encapsulated components within the class and access them in the application. There are three different approaches through which we can access TypeScript private 3 min read TypeScript Less Common Primitives Type TypeScript Less Common Primitives Type offers a rich set of primitive types to represent data. While most developers are familiar with types like number, string, boolean, and symbol, TypeScript also provides less common primitive types that can be incredibly useful in specific scenarios. These are s 2 min read How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar 3 min read How to Access Enum Values in TypeScript ? Enums are a feature in TypeScript that help organize collections of related values. Accessing these values efficiently is crucial for clean, maintainable code. This guide provides a straightforward overview of different methods to access enum values in TypeScript, including bracket notation, dot not 3 min read Like