TypeScript Interview Questions

Answer: TypeScript is an open-source programming language that serves as a superset of JavaScript, which can be compiled into pure JavaScript. It enhances code reliability and maintainability through static type checking, allowing developers to detect common errors such as typos or type mismatches during the compilation phase. Additionally, TypeScript provides features for object-oriented programming, including interfaces, classes, and namespaces, making code more modular and readable.

What are the types in TypeScript? How do you define variable types?

Answer: The types available in TypeScript include primitive types (such as strings, numbers, booleans, etc.), object types (such as arrays, tuples, enums, functions, etc.), and special types (such as any, void, never, unknown, etc.). To define a variable's type, you can use the colon followed by the type name to specify it. For example:

let myString: string = "Hello, world!";
let myNumber: number = 42;
let myBoolean: boolean = true;
let myArray: number[] = [1, 2, 3];
let myTuple: [string, number] = ["hello", 10];
let myEnum: enum Color { Red, Green, Blue };
function myFunction(a: number, b: number): number {
    return a + b; // Return the sum of two numbers.
}

What is an interface in TypeScript? How do you define interfaces?

Answer: An interface is an abstract data type used to describe object properties and methods. In TypeScript, interfaces can be used to define object types for compile-time type checking. To define an interface, use the interface keyword. For example:

interface Person {
    name: string;
    age: number;
    sayHello(): void; // Method signature indicating no parameters and returning nothing (void).
}

The above code defines a Person interface containing two properties (name, age) and one method named sayHello that takes no arguments and returns no value. You can create an object conforming to this interface definition by implementing it, for example:

let person: Person = {
    name: "Alice",
    age: 30,
    sayHello() { // Implement the method logic here.
        console.log("Hello, my name is " + this.name);
    }
};

What are classes in TypeScript? How do you define them?

Answer: A class is an object-oriented programming concept used to describe objects with similar properties and methods. In TypeScript, a class can be defined using the class keyword. For example:

class Person {
    name: string; // Property declaration without initialization by default in classes (unless specified otherwise).
    age: number;
    
    constructor(name: string, age: number) {
        this.name = name; // Initialize properties using 'this'.
        this.age = age;
    }

    sayHello() {
        console.log("Hello, my name is " + this.name); // Method implementation.
    }
}

The above code defines a Person class with two properties (name, age) and one method named sayHello. You can use it by creating an instance of the class:

let person = new Person("Alice", 30); // Create a new instance.
person.sayHello(); // Outputs "Hello, my name is Alice".

What are namespaces in TypeScript? How do you define them?

Answer: A namespace is a way of organizing code into logical units to avoid naming conflicts and duplication. In TypeScript, a namespace can be defined using the namespace keyword. For example:

namespace MyNamespace {
    export const myVariable = "hello"; // Exported constant declaration.
    
    export function myFunction() {
        console.log("world"); // Exported method implementation.
    }
}

The above code defines a namespace named MyNamespace containing an exported variable (myVariable) and an exported method (myFunction). Using the export keyword allows these members to be used in other files, for example:

/// <reference path="MyNamespace.ts" /> // Import reference directive.
console.log(MyNamespace.myVariable); // Outputs "hello".
MyNamespace.myFunction(); // Outputs "world".

What are modules in TypeScript? How do you define them?

Answer: A module is a way of organizing code into independent, reusable units to improve maintainability and reusability. In TypeScript, modules can be defined using the import and export keywords. For example:

// MyModule.ts
export const myVariable = "hello"; // Exported constant declaration in module file.

export function myFunction() {
    console.log("world"); // Exported method implementation in module file.
}

// main.ts
import { myVariable, myFunction } from "./MyModule"; // Import statements using relative path.
console.log(myVariable); // Outputs "hello".
myFunction(); // Outputs "world".

The above code defines a MyModule module containing an exported variable (myVariable) and an exported method (myFunction). In main.ts, these members are imported via the import keyword using relative paths, allowing them to be used within that file. Note: Relative paths must be used when referencing other module files in TypeScript.