0
0
Typescriptprogramming~10 mins

Parameter properties shorthand in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parameter properties shorthand
Define class with constructor
Use parameter properties shorthand
Parameters auto become class properties
Create instance with values
Access properties directly
This flow shows how using parameter properties shorthand in a constructor automatically creates and assigns class properties from parameters.
Execution Sample
Typescript
class Person {
  constructor(public name: string, private age: number) {}
}
const p = new Person('Alice', 30);
console.log(p.name);
This code defines a class using parameter properties shorthand, creates an instance, and logs a public property.
Execution Table
StepActionParameterProperty CreatedValue AssignedOutput
1Constructor calledname='Alice', age=30name (public), age (private)name='Alice', age=30
2Instance createdProperties set on instancename='Alice', age=30
3Access public propertynameAliceAlice
4Try to access private propertyageError if accessed outside classError
5EndExecution complete
💡 Constructor finishes, instance properties are set, output shows public property value.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameundefined'Alice''Alice''Alice'
ageundefined303030
p (instance)undefinedinstance createdinstance with propertiesinstance with properties
Key Moments - 2 Insights
Why can we access p.name but not p.age?
Because 'name' is declared public, it becomes accessible outside the class. 'age' is private, so it cannot be accessed outside, as shown in execution_table step 4.
Does the constructor need to assign parameters to properties manually?
No, parameter properties shorthand automatically creates and assigns properties, so no manual assignment is needed, as seen in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does the property 'name' have after step 2?
A30
Bundefined
C'Alice'
DError
💡 Hint
Check the 'Property Created' and 'Value Assigned' columns in step 2.
At which step does the instance 'p' get created with properties?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing instance creation.
If 'age' was declared public instead of private, what would change in the execution?
AWe could access p.age without error.
BConstructor would not assign age.
CProperty 'name' would become private.
DNo change at all.
💡 Hint
Refer to the explanation about access modifiers in key_moments.
Concept Snapshot
Parameter properties shorthand in TypeScript lets you declare and initialize class properties directly in constructor parameters.
Syntax: constructor(public name: string, private age: number) {}
This saves writing separate property declarations and assignments.
Public properties are accessible outside the class; private are not.
It makes class code shorter and clearer.
Full Transcript
This visual execution trace shows how TypeScript's parameter properties shorthand works. When a class constructor uses access modifiers like public or private in its parameters, TypeScript automatically creates class properties with those names and assigns the passed values. For example, in the Person class, the constructor parameters 'public name' and 'private age' create properties 'name' and 'age' on the instance. When we create an instance with new Person('Alice', 30), the properties are set automatically. Accessing p.name works because it is public, but accessing p.age outside the class causes an error because it is private. This shorthand saves us from writing separate property declarations and assignments inside the constructor. The execution table shows each step: constructor call, instance creation, property access, and error on private property access. The variable tracker shows how 'name' and 'age' get their values. The key moments clarify common confusions about access and assignment. The quiz tests understanding of property values, instance creation, and access modifiers. Overall, parameter properties shorthand is a neat way to write concise and clear class constructors in TypeScript.