What if you could avoid messy code and bugs just by declaring your class properties clearly from the start?
Why Class property declarations in Typescript? - Purpose & Use Cases
Imagine you are building a program to manage a library. You want to keep track of each book's title, author, and number of pages. Without class property declarations, you have to manually create and manage these properties inside the constructor or methods, which can get messy and confusing.
Manually adding properties inside the constructor means repeating code and risking mistakes like typos or forgetting to initialize a property. It also makes your code longer and harder to read, especially when you have many properties to manage.
Class property declarations let you define all the properties your class will have right at the top. This makes your code cleaner, easier to understand, and reduces errors because you clearly see what data each object will hold.
class Book {
title: string;
author: string;
pages: number;
constructor(title: string, author: string, pages: number) {
this.title = title;
this.author = author;
this.pages = pages;
}
}class Book { title = ''; author = ''; pages = 0; constructor(title: string, author: string, pages: number) { this.title = title; this.author = author; this.pages = pages; } }
It enables you to write clearer and safer classes that are easier to maintain and understand.
Think of a contact app where each contact has a name, phone number, and email. Declaring these properties upfront helps you quickly see what information each contact holds and avoid mistakes when adding new contacts.
Declaring class properties upfront makes your code cleaner.
It reduces errors by clearly showing what data each object holds.
It helps maintain and understand your code better as it grows.
Practice
Solution
Step 1: Understand what class properties represent
Class properties hold data or information specific to each object created from the class.Step 2: Differentiate properties from methods and other elements
Functions inside classes are called methods, not properties. Properties are for storing values.Final Answer:
To store data related to each object created from the class -> Option DQuick Check:
Class properties = store object data [OK]
- Confusing properties with methods
- Thinking properties create new classes
- Believing properties import modules
Solution
Step 1: Recall TypeScript property syntax
Properties are declared asname: type = defaultValue;.Step 2: Check each option for correct syntax
class Car { speed: number = 0; } matches the correct syntax:speed: number = 0;. Others have incorrect order or missing punctuation.Final Answer:
class Car { speed: number = 0; } -> Option BQuick Check:
Property syntax = name: type = value [OK]
- Swapping type and value order
- Missing colon between name and type
- Using wrong punctuation
class Person {
name: string = "Alice";
age: number = 30;
}
const p = new Person();
console.log(p.name + ' is ' + p.age + ' years old');Solution
Step 1: Understand property initialization
The class Person has propertiesnameandageinitialized with "Alice" and 30 respectively.Step 2: Check the output of console.log
Creating an objectpsets these properties. The log concatenates them into the string "Alice is 30 years old".Final Answer:
"Alice is 30 years old" -> Option AQuick Check:
Initialized properties print correct values [OK]
- Assuming properties are undefined without constructor
- Confusing property names or types
- Expecting runtime errors without syntax mistakes
class Book {
title string = "Unknown";
pages: number = 100;
}Solution
Step 1: Check syntax of property declarations
Property declarations require a colon ':' between the name and type, liketitle: string.Step 2: Identify the incorrect line
The linetitle string = "Unknown";misses the colon, causing a syntax error.Final Answer:
Missing colon ':' between property name and type in 'title string = "Unknown";' -> Option CQuick Check:
Property syntax requires colon ':' [OK]
- Omitting colon between name and type
- Thinking default values are not allowed
- Ignoring case sensitivity of class names (not an error)
Student with a property grades that stores an array of numbers and defaults to an empty array. Which is the correct declaration to ensure each object has its own separate array?Solution
Step 1: Understand instance vs static properties
Instance properties belong to each object separately; static properties are shared by all objects.Step 2: Check which option creates separate arrays per object
class Student { grades: number[] = []; } declaresgradesas an instance property with default empty array, so each object gets its own array.Step 3: Analyze other options
class Student { static grades: number[] = []; } uses static, so all objects share one array (not desired). class Student { grades: number[]; } // no default value has no default, so array is undefined unless set later. class Student { grades = []; } misses type annotation explicitly, inferring any[] instead of number[].Final Answer:
class Student { grades: number[] = []; } -> Option AQuick Check:
Instance property with default array = separate arrays [OK]
- Using static for properties needing separate values
- Not initializing arrays causing undefined errors
- Omitting type annotation (less clear)
Ready to prove it?
Pass the test and this pattern moves unknown
