TypeScript Flashcards

(25 cards)

1
Q

What is TypeScript?

A

TypeScript is a strongly typed, object-oriented superset of JavaScript that compiles to plain JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the main benefit of using TypeScript?

A

It catches type-related errors at compile-time rather than runtime, enhancing code quality and developer productivity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you define a variable with a specific type (type annotation)?

A

let username: string = “Alice”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is type inference in TypeScript?

A

The compiler automatically determines the type of a variable based on its assigned value, even if no explicit type is declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the type do?

A

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

How do you mark a function parameter as optional?

A

By adding a question mark after the parameter name (e.g., function greet(name?: string) ).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is used for in TypeScript?

A

It is used as the return type for functions that do not return a value. [1, 2, 3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between and ?

A

Interfaces are ideal for defining object shapes and can be merged (declaration merging). Types are more flexible, supporting union types, primitives, and tuples.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Union Type?

A

It allows a variable to be one of several types, defined using the character (e.g., ).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a Tuple in TypeScript?

A

An array with a fixed number of elements where each element has a known, specific type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between and ?

A

is an assigned value representing no value, while means a variable has been declared but not yet assigned a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of ?

A

It takes an object type and produces a string or numeric literal union of its keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are Generics used for?

A

To create reusable components that work with a variety of types rather than a single one, providing type safety without sacrificing flexibility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the modifier do?

A

It prevents a property from being reassigned after initialization. [2, 3, 6, 7, 8, 9]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the file?

A

A configuration file that specifies the root files and compiler options for a TypeScript project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does do?

A

It enables all strict type-checking options, which strongly improves type safety.

17
Q

What is the type?

A

A safer alternative to that forces you to perform type checking or casting before using the value.

18
Q

What is the type?

A

It represents values that never occur, such as a function that always throws an error or an infinite loop.

19
Q

What are Type Guards?

A

Techniques like or used in statements to narrow down the type of a variable within a conditional block.

20
Q

What is an Enum?

A

A way to define a set of named constants (numeric or string).

21
Q

What is a Utility Type?

A

Built-in types that facilitate common type transformations, such as , , , or . [1, 2, 3, 10, 11]

22
Q

How do you declare an array of numbers?

A

let numbers: number[] = [1, 2, 3, 4, 5];

23
Q

How do you declare a function type?

A

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;

24
Q

How do you cast a variable in TypeScript?

A

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;

25
What is the Nullish Coalescing operator ?
It returns the right-hand operand when the left-hand operand is or , but not for other falsy values like or . [1, 6, 12, 13, 14]