TypeScript Flashcards

(5 cards)

1
Q

Is static typing faster than dynamic typing?

A

Static Typing: Types are known at compile time, e.g. TypeScript
Dynamic Typing: Types are checked at runtime, e.g. JavaScript

It depends, but dynamic typing can usually be slower due to runtime checks.

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

What is the difference between a type and an interface in TypeScript?

A

Interface: Primarily designed to describe the shape of object types (including function signatures).

Type alias: More general — can represent object types and other types like primitives, unions, intersections, tuples, etc.

Types:
1. Can describe primitives.
2. Can describe unions or intersections.
3. Can NOT be extended or implemented, but you can create intersections.
4. Don’t allow declaration merging.
5. Support computer properties and mapped types.
6. Are preferred for flexible types and unions, as opposed to public APIs and OOP patterns.

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

What is the difference between “Unknown,” “Any” and “Never” in TypeScript?

A
  1. any: Disables TypeScript’s type safety for that variable.
  2. unknown: Does NOT disable TypeScript’s type safety for that variable. Forces you to narrow down the type before using it.
  3. never: Represents a type that never occurs. Used in exhaustiveness checks in switch statements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the five key data structures in TypeScript?

A
  1. Object (Interface / Type)
  2. Array
  3. Tuple
  4. Set
  5. Map
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the advantages and disadvantages of generics?

A

Advantages:
1. Type Safety: Catch type errors at compile time while maintaining flexibility.
2. Code Reusability: One generic function or class can work with multiple types.
3. Better Intellisense and Autocomplete: TypeScript can infer types from generics, helping tools provide better suggestions and reducing bugs.
4. Avoids Redundant Overloads: You don’t need to write multiple versions of a function for different types.
5. Enforces Constraints: You can restrict what kinds of types a generic can accept using extends.

Disadvantages:
1. Added Complexity: The syntax can be intimidating to beginners or in complex codebases.
2. More Indirect Code: Reading generic-heavy code requires understanding how types flow through functions or components.
3. Verbose or Overused: Sometimes generics are used where simpler types or union types would suffice, making code harder to read.
4. Type Inference Isn’t Always Perfect: In complex scenarios, TypeScript may fail to infer the right type, requiring manual type annotations.
5. Runtime Limitations: Generics are erased at runtime — they are only available at compile time.

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