TypeScript Flashcards
How do you declare a variable with a specific type in TypeScript?
Use the let or const keyword followed by the variable name and a type annotation. For example:
let name: string; const age: number = 30;
What is the syntax for defining a function with typed parameters and a return type in TypeScript?
Define the function with parameter types and a return type. For example:
function sum(lhs: number, rhs: number): number { return lhs + rhs; }
How do you create an array of numbers in TypeScript?
Use square brackets with the type number. For example:
const numbers: number[] = [1, 2, 3];
What is a tuple in TypeScript, and how do you define one?
A tuple is an array with a fixed number of elements of specified types. Define it using square brackets with types. For example:
let book: [string, number]; book = ["sample", 1980];
How do you define a class with private, protected, and public properties in TypeScript?
Use the class keyword and specify access modifiers for each property. For example:
class Dimension { private length: number; protected width: number; public height: number; constructor(l: number, w: number, h: number) { this.length = l; this.width = w; this.height = h; } getLength(): number { return this.length; } }
What is an interface in TypeScript, and how do you implement it in a class?
An interface defines a contract for object shapes. Implement it in a class using the implements keyword. For example:
interface Area { area(): number; } class Rectangle implements Area { constructor(public length: number, public width: number) {} area(): number { return this.length * this.width; } }
How do you handle exceptions in TypeScript?
Use try…catch blocks to handle exceptions. For example:
try { // code that may throw an error } catch (e) { console.error(`An error occurred: ${e}`); }
What is optional chaining in TypeScript, and how is it used?
Optional chaining allows safe access to nested object properties that might be null or undefined. Use the ?. operator. For example:
console.log(result?.pii?.age);
How do you define a generic function in TypeScript?
Use angle brackets to specify a generic type parameter. For example:
function getFirst<T>(arr: T[]): T | undefined { return arr.length > 0 ? arr[0] : undefined; }
What is the purpose of utility types like Pick and Omit in TypeScript?
Utility types allow you to create new types by selecting or omitting properties from existing types. For example:
interface UserForm { email: string; password: string; phoneNumber?: string; } type LoginForm = Pick<UserForm, "email" | "password">; type ContactForm = Omit<UserForm, "password">;
What is type assertion in TypeScript, and how is it used?
Type assertion tells the compiler to treat a variable as a specific type. Use as or angle bracket syntax. For example:
let someValue: any = "Hello, world!"; let strLength: number = (someValue as string).length;
What are union types in TypeScript?
Union types allow a variable to hold one of several types. Use the | symbol. For example:
let value: string | number; value = "hello"; value = 42;
How do you create a Readonly object in TypeScript?
Use the Readonly utility type to make all properties of an object immutable. For example:
interface User { name: string; age: number; } const user: Readonly<User> = { name: "John", age: 30 }; // user.name = "Jane"; // Error: Cannot assign to 'name'
How do enums work in TypeScript?
Enums define a set of named constants. Use enum. For example:
enum Color { Red, Green, Blue, } let c: Color = Color.Green;
What is a mapped type in TypeScript?
Mapped types create new types by transforming properties of an existing type. For example:
type Readonly<T> = { readonly [P in keyof T]: T[P]; };