Simple ClassProblem Statement'Define a class Point with x and y as numbers and a constructor to initialize them.'Approach 1. Declare properties with types. 2. Initialize them in the constructor. Solution`class Point { x: number; y: number; constructor(x: number, y: number) { this.x = x; this.y = y;
Private ModifiersProblem Statement'In the Point class, make x and y private.'Approach 1. Use the `private` keyword before the property names. Solution`class Point { private x: number; private y: number; constructor(x: number, y: number) { this.x = x; this.y = y;
Parameter PropertiesProblem Statement'Refactor the Point class to use the parameter properties shorthand in the constructor.'Approach 1. Use a modifier directly in the constructor parameters. Solution`class Point { constructor(private x: number, private y: number) {
Public MethodProblem Statement'Add a public method getCoords() to the Point class that returns [x, y].'Approach 1. Methods are public by default, but you can be explicit. Solution`class Point { constructor(private x: number, private y: number) {
Readonly PropertyProblem Statement'Make a property id readonly in a User class.'Approach 1. Use `readonly` in the declaration or constructor shortcut. Solution`class User { constructor(public readonly id: number, public name: string) {
Class InheritanceProblem Statement'Create a class Animal with move() and a subclass Dog that overrides move().'Approach 1. Use `extends`. 2. Use `super` if needed, but not required if just overriding. Solution`class Animal { move() { console.log("Moving...");
Protected ModifierProblem Statement'Explain why you would use "protected" instead of "private".'Approach 1. `protected` allows access in subclasses, `private` does not. Solution`class Base { protected id: number = 0;
Implementing InterfacesProblem Statement'Create an interface Shape with an area() method. Implement it in a Circle class.'Approach 1. Use the `implements` keyword. Solution`interface Shape { area(): number;
Abstract ClassProblem Statement'Define an abstract class Vehicle with an abstract method start().'Approach 1. Use `abstract` for the class and method. 2. Abstract methods have no body. Solution`abstract class Vehicle { abstract start(): void; stop() { console.log("Stopped");
Static PropertyProblem Statement'Add a static property count to a User class to track instances.'Approach 1. Use the `static` keyword. Solution`class User { static count = 0; constructor() { User.count++;
Getters and SettersProblem Statement'Create a Square class with a size property and a getter for area.'Approach 1. Use `get` keyword. Solution`class Square { constructor(public size: number) {
Setter ValidationProblem Statement'Add a setter for size that ensures the value is never negative.'Approach 1. Use `set` keyword. Solution`class Square { private _size: number = 0; set size(value: number) { if (value >= 0) this._size = value;
Returning thisProblem Statement'Use "this" as a return type for chaining methods in a Calculator class.'Approach 1. Return `this` for fluent APIs. Solution`class Calculator { value = 0; add(n: number): this { this.value += n; return this;
Index Signature in ClassProblem Statement'Can you add an index signature to a class? Show an example.'Approach 1. Yes, usually used for dynamic properties. Solution`class MyDict { [key: string]: string | number; name: string = "Dict";
Interface vs ClassProblem Statement'Can a class implement multiple interfaces? Show the syntax.'Approach 1. Yes, separate with commas. Solution`interface Flyable { fly(): void
Constructor TypesProblem Statement'What is the type of a class constructor?'Approach 1. A constructor type looks like `new (...args: any[]) => T`. Solution`type ClassType<T> = new (...args: any[]) => T; function createInstance<T>(cls: ClassType<T>): T { return new cls();
ES Private vs TS PrivateProblem Statement'Show the syntax for JavaScript native private fields (#).'Approach 1. Use `#` prefix. Solution`class User { #password: string; constructor(pass: string) { this.#password = pass;
Method OverloadingProblem Statement'Implement an overloaded method log(msg: string) and log(err: Error).'Approach 1. Define signatures then the implementation. Solution`class Logger { log(msg: string): void; log(err: Error): void; log(val: string | Error) { console.log(val);
Class Geenric ConstraintProblem Statement'Create a Generic class DataStore<T extends { id: numberApproach 1. Ensures `T` always has an `id`. Solution`class DataStore<T extends { id: number
Class as InterfaceProblem Statement'Explain why a class can be used as an interface in TS.'Approach 1. Classes create both a type and a value. Solution`class Point { x = 0; y = 0;