Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Public Members | Access Modifiers in TypeScript
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookPublic Members

In TypeScript, the public access modifier is used to specify that a class property or method can be accessed from anywhere: inside the class, by instances of the class, and even from code outside the class. In fact, if you do not explicitly specify an access modifier for a class member, TypeScript treats it as public by default. This means your class members are open for access unless you intentionally restrict them.

12345678910111213
class Car { public make: string; public model: string; public start(): void { console.log(`${this.make} ${this.model} is starting.`); } } const myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Corolla"; myCar.start(); // Output: Toyota Corolla is starting.
copy

Using public members is useful when you want to allow other parts of your code to interact freely with your class. For example, if you are building a data model where all properties should be accessible and modifiable, declaring them as public makes your intent clear and keeps your code straightforward. However, you should use public members thoughtfully to avoid exposing implementation details that might be better kept private or protected in future code changes.

1. What is the default access modifier for class members in TypeScript if none is specified?

2. Fill in the blank to declare a public property in a class:

question mark

What is the default access modifier for class members in TypeScript if none is specified?

Select the correct answer

question-icon

Fill in the blank to declare a public property in a class:

name: string;

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain the difference between public, private, and protected in TypeScript?

When should I use public instead of private or protected?

Are there any best practices for using public members in TypeScript classes?

Awesome!

Completion rate improved to 5

bookPublic Members

Deslize para mostrar o menu

In TypeScript, the public access modifier is used to specify that a class property or method can be accessed from anywhere: inside the class, by instances of the class, and even from code outside the class. In fact, if you do not explicitly specify an access modifier for a class member, TypeScript treats it as public by default. This means your class members are open for access unless you intentionally restrict them.

12345678910111213
class Car { public make: string; public model: string; public start(): void { console.log(`${this.make} ${this.model} is starting.`); } } const myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Corolla"; myCar.start(); // Output: Toyota Corolla is starting.
copy

Using public members is useful when you want to allow other parts of your code to interact freely with your class. For example, if you are building a data model where all properties should be accessible and modifiable, declaring them as public makes your intent clear and keeps your code straightforward. However, you should use public members thoughtfully to avoid exposing implementation details that might be better kept private or protected in future code changes.

1. What is the default access modifier for class members in TypeScript if none is specified?

2. Fill in the blank to declare a public property in a class:

question mark

What is the default access modifier for class members in TypeScript if none is specified?

Select the correct answer

question-icon

Fill in the blank to declare a public property in a class:

name: string;

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
some-alt