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?
string | number, to specify that a variable 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.
type Point = { x: number, y: number };How can you create and use conditional types in TypeScript?
extends keyword to create type branches based on existing types.What is the difference between “interface” and “class” in TypeScript?
What is the purpose of the “readonly” modifier in TypeScript?
readonly modifier indicates that a property or array cannot be modified after it’s initialized.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?
namespace keyword.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.
super keyword is used to call methods or access properties of a parent class in a derived class.What is the purpose of the “private,” “protected,” and “public” access modifiers in TypeScript classes?
How can you implement interface inheritance in TypeScript?
extends keyword to make one interface inherit the properties and methods of another.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?
as keyword or angle brackets.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?
this type is used to represent the type of this within a function. It helps with type checking in functions that rely on this.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?
as const is used to infer literal types from object literals, making all properties read-only and preserving their exact values.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?
any, unknown, and never are three distinct types that serve different purposes in the type system.any:
any type is the most permissive type in TypeScript. It essentially disables static type checking for a particular value or expression.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.
unknown:
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.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.
never:
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.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.
any provides maximum flexibility but sacrifices type safety, unknown adds a layer of type safety by requiring explicit type checks, and never is used to indicate values or functions that will never occur or complete. The choice between them depends on the specific requirements and design goals of your code.