TypeScript Flashcards

1
Q

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

A

Use the let or const keyword followed by the variable name and a type annotation. For example:

let name: string;
const age: number = 30;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax for defining a function with typed parameters and a return type in TypeScript?

A

Define the function with parameter types and a return type. For example:

function sum(lhs: number, rhs: number): number {
  return lhs + rhs;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create an array of numbers in TypeScript?

A

Use square brackets with the type number. For example:

const numbers: number[] = [1, 2, 3];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a tuple in TypeScript, and how do you define one?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you define a class with private, protected, and public properties in TypeScript?

A

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;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an interface in TypeScript, and how do you implement it in a class?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you handle exceptions in TypeScript?

A

Use try…catch blocks to handle exceptions. For example:

try {
  // code that may throw an error
} catch (e) {
  console.error(`An error occurred: ${e}`);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is optional chaining in TypeScript, and how is it used?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you define a generic function in TypeScript?

A

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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of utility types like Pick and Omit in TypeScript?

A

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">;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is type assertion in TypeScript, and how is it used?

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are union types in TypeScript?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create a Readonly object in TypeScript?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do enums work in TypeScript?

A

Enums define a set of named constants. Use enum. For example:

enum Color {
  Red,
  Green,
  Blue,
}

let c: Color = Color.Green;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a mapped type in TypeScript?

A

Mapped types create new types by transforming properties of an existing type. For example:

type Readonly<T> = {
  readonly [P in keyof T]: T[P];
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are type guards in TypeScript?

A

Type guards are functions or constructs that allow you to narrow types during runtime checks. For example:

function isString(value: any): value is string {
  return typeof value === "string";
}
16
Q

What is the never type in TypeScript?

A

The never type represents values that never occur, often used in unreachable code. For example:

function throwError(message: string): never {
  throw new Error(message);
}
17
Q

How do you use decorators in TypeScript?

A

Decorators are special declarations prefixed with @ that can modify classes or members. For example:

function Log(target: any, key: string) {
  console.log(`${key} was called`);
}

class Example {
  @Log
  method() {}
}
18
Q

What are index signatures in TypeScript?

A

Index signatures define the type of keys and their corresponding values for an object. For example:

interface StringDictionary {
  [key: string]: string;
}

let dict: StringDictionary = { hello: "world", goodbye: "everyone" };
19
Q

What is the purpose of the Partial utility type in TypeScript?

A

Partial makes all properties of a type optional. For example:

interface User {
name: string;
age: number;
}

const partialUser: Partial<User> = { name: "Alice" };</User>