Introducing TypeScript Flashcards

1
Q

Why can’t TypeScript run directly in a browser?

A

It must be transpiled into JavaScript first.

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

What is the main benefit of TypeScript over JavaScript?

A

It adds a rich type system to JavaScript.

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

What is the purpose of the unknown type in TypeScript?

A

It is used when the type is uncertain but requires type safety.

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

What must be done before using a variable of type unknown?

A

It must be explicitly checked before use.

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

What is a type predicate in TypeScript?

A

A function that verifies a value’s type.

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

How do you define a custom type in TypeScript?

A

Using the type keyword.

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

What operator is used to extend types in TypeScript?

A

The & (intersection) operator.

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

How do you define a type alias for a product with an optional price in TypeScript?

A

type Product = { name: string; unitPrice?: number };

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

How do you create a DiscountedProduct type that extends Product?

A

type DiscountedProduct = Product & { discount: number };

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

What does the following TypeScript snippet do?

fetch("https://swapi.dev/api/people/1")
  .then((response) => response.json())
  .then((data: unknown) => {
    if (isCharacter(data)) {
      console.log("name", data.name);
    }
  });
A

It fetches data from an API, ensures type safety by using unknown, and checks the type before accessing properties.

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