tech-interview

What is TypeScript, and how does it relate to JavaScript?

What are the benefits of using TypeScript?

How do you declare a variable with a specific type in TypeScript?

let age: number = 30;

What are TypeScript interfaces, and how are they used?

How can you ensure that a variable in TypeScript can have multiple types?

What is type inference in TypeScript?

Explain the concept of “type assertions” in TypeScript.

What are the different types of modules in TypeScript, and how do they differ?

How can you create and use decorators in TypeScript?

What are enums in TypeScript, and when would you use them?

Explain the difference between “interface” and “type” in TypeScript.

How does TypeScript handle optional properties in interfaces?

interface Person {
  name?: string;
}

What is a generic type in TypeScript, and why is it useful?

What are type guards, and how do they work in TypeScript?

What is the “never” type in TypeScript, and when is it commonly used?

How can you enforce strict null checks in TypeScript?

Explain what “type alias” is in TypeScript and give an example.

How can you create and use conditional types in TypeScript?

What is the difference between “interface” and “class” in TypeScript?

What is the purpose of the “readonly” modifier in TypeScript?

How do you handle asynchronous operations in TypeScript?

What is an abstract class in TypeScript, and when would you use one?

How can you define and use namespaces in TypeScript?

What is a “declaration file” in TypeScript, and when is it used?

How does TypeScript handle type checking in classes and inheritance?

Explain the “super” keyword in TypeScript when working with classes.

What is the purpose of the “private,” “protected,” and “public” access modifiers in TypeScript classes?

How can you implement interface inheritance in TypeScript?

What is the “keyof” keyword in TypeScript, and how is it used?

How can you use TypeScript with React for type safety?

What are “discriminated unions” in TypeScript, and how are they useful?

How can you define and use type guards with discriminated unions?

What are “module augmentation” and “declaration merging” in TypeScript?

How can you configure TypeScript options in a tsconfig.json file?

Explain the “tsc” command and how to use it to compile TypeScript files.

What is the purpose of the “any” type in TypeScript?

How can you define and use type assertions in TypeScript?

Explain the role of “tsconfig.json” in a TypeScript project.

What is a “namespace import” and a “default import” in TypeScript module systems?

How can you create and use decorators with class methods in TypeScript?

What is the “this” type in TypeScript, and how does it help with type checking?

Explain the “strict” mode in TypeScript and its advantages.

How can you handle external dependencies in TypeScript, such as third-party libraries?

What is the purpose of “template literal types” in TypeScript, and how do they work?

Explain the concept of “tsconfig.json” inheritance and how it works.

How do you ensure type safety when working with third-party JavaScript libraries in TypeScript?

What is the “as const” assertion in TypeScript, and how does it work?

Explain the use of “mapped types” in TypeScript and provide an example.

How can you use TypeScript with popular build tools like Webpack or Babel?

**What is the difference between any, unknown and never?

  1. any:
    • Description: The any type is the most permissive type in TypeScript. It essentially disables static type checking for a particular value or expression.
    • Use Cases: Use any when you want to opt out of TypeScript’s type checking for a specific variable or value. It is often used when migrating JavaScript code to TypeScript or when dealing with dynamic or uncertain types.
    let someValue: any = "Hello, World!";
    console.log(someValue.length); // No type checking errors, even though length is not a property of all values
    

    While any provides flexibility, it comes at the cost of losing the benefits of static type checking.

  2. unknown:
    • Description: The unknown type is a safer alternative to any. Variables of type unknown require a type assertion or a type check before they can be used.
    • Use Cases: Use unknown when the type of a value is not known at compile time, and you want to enforce type checking before performing operations on that value.
    let userInput: unknown = getUserInput();
       
    // Type assertion
    let userName: string = userInput as string;
    
    // Type check
    if (typeof userInput === "string") {
        let userName: string = userInput;
    }
    

    unknown encourages you to perform type checking before using values, making the code more robust.

  3. never:
    • Description: The never type represents values that will never occur. It is often used to indicate functions that throw exceptions, enter infinite loops, or have unreachable endpoints.
    • Use Cases: Use never when you have a function that never returns (e.g., throws an error or enters an infinite loop).
    function throwError(message: string): never {
        throw new Error(message);
    }
    
    function infiniteLoop(): never {
        while (true) {
            // code that never exits
        }
    }
    

    never is useful in scenarios where the function is not expected to complete normally.