Skip to main content

Classes & Objects

Ayesha
EditReport

TypeScript features comprehensive Object-Oriented structures, adding strict encapsulation modifiers and type properties over traditional JavaScript classes.

Video Explanation​

1. Class Structure and Properties​

class NetworkDevice {
// Explicit field type definitions
public deviceId: number;
public deviceModel: string;

// Execution constructor method configuration
constructor(id: number, model: string) {
this.deviceId = id;
this.deviceModel = model;
}

public syncTelemetry(): void {
console.log(`Syncing tracking nodes for device ${this.deviceId}.`);
}
}

// Instantiating concrete object allocations
const gatewayRouter = new NetworkDevice(881, "Cisco ISR");
gatewayRouter.syncTelemetry();

2. Property Encapsulation Access Modifiers​

TypeScript provides three explicit visibility modifiers to protect class properties from unintended external modifications:

  • public: (Default) Properties can be accessed from any file or outer runtime context.
  • private: Access is strictly confined to the defining class boundary itself.
  • protected: Access is shared with subclasses inheriting from this parent class.
class BankVault {
public vaultLabel: string;
private combinationSequence: number[];
protected emergencyOverrideToken: string;

constructor(label: string, combination: number[], token: string) {
this.vaultLabel = label;
this.combinationSequence = combination;
this.emergencyOverrideToken = token;
}
}

3. Parameter Properties (Shorthand Syntax)​

You can compress constructor logic by declaring visibility access modifiers directly inside the arguments signature list. This tells the compiler to automatically declare and assign those fields for you:

class CompressedUser {
// Declares, initializes, and maps fields completely in one line
constructor(public username: string, private accessLevel: number) {}
}
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

Was this page helpful?

💬

Discuss this page

Have a question or spot something confusing in "Classes & Objects"? Ask below. Backed by GitHub Discussions—maintainers receive system notifications directly.