What is TypeScript?
TypeScript is a strongly typed, object-oriented superset of JavaScript that compiles to plain JavaScript.
What is the main benefit of using TypeScript?
It catches type-related errors at compile-time rather than runtime, enhancing code quality and developer productivity.
How do you define a variable with a specific type (type annotation)?
let username: string = “Alice”;
What is type inference in TypeScript?
The compiler automatically determines the type of a variable based on its assigned value, even if no explicit type is declared.
What does the type do?
the type keyword is used to create type aliases, which provide a new name for any existing type, including primitives, complex object shapes, unions, and more. This helps developers write safer, more readable, and maintainable code by leveraging static type checking.
How do you mark a function parameter as optional?
By adding a question mark after the parameter name (e.g., function greet(name?: string) ).
What is used for in TypeScript?
It is used as the return type for functions that do not return a value. [1, 2, 3, 4, 5]
What is the difference between and ?
Interfaces are ideal for defining object shapes and can be merged (declaration merging). Types are more flexible, supporting union types, primitives, and tuples.
What is a Union Type?
It allows a variable to be one of several types, defined using the character (e.g., ).
What is a Tuple in TypeScript?
An array with a fixed number of elements where each element has a known, specific type.
What is the difference between and ?
is an assigned value representing no value, while means a variable has been declared but not yet assigned a value.
What is the purpose of ?
It takes an object type and produces a string or numeric literal union of its keys.
What are Generics used for?
To create reusable components that work with a variety of types rather than a single one, providing type safety without sacrificing flexibility.
What does the modifier do?
It prevents a property from being reassigned after initialization. [2, 3, 6, 7, 8, 9]
What is the file?
A configuration file that specifies the root files and compiler options for a TypeScript project.
What does do?
It enables all strict type-checking options, which strongly improves type safety.
What is the type?
A safer alternative to that forces you to perform type checking or casting before using the value.
What is the type?
It represents values that never occur, such as a function that always throws an error or an infinite loop.
What are Type Guards?
Techniques like or used in statements to narrow down the type of a variable within a conditional block.
What is an Enum?
A way to define a set of named constants (numeric or string).
What is a Utility Type?
Built-in types that facilitate common type transformations, such as , , , or . [1, 2, 3, 10, 11]
How do you declare an array of numbers?
let numbers: number[] = [1, 2, 3, 4, 5];
How do you declare a function type?
you declare a function type by defining the shape of a function—its parameters and its return type—using an arrow function-like syntax:
(parameterName: type) => returnType
// Declaring a variable with a function type
let add: (a: number, b: number) => number;
// Assigning a function to that variable
add = (x, y) => x + y;
How do you cast a variable in TypeScript?
The primary way to cast a variable is by using the ‘as’ keyword, which is preferred for readability and compatibility with JSX.
value: unknown = “Hello World”; let len: number = (value as string).length;