TS Classes, types, keywords, etc. Flashcards

(11 cards)

1
Q

What is the static keyword in TypeScript Classes?

A
  • Definition: Defines members belonging to the Class constructor, not the instance.
  • Access: Called via ClassName.member (not this.member).
  • Memory: Exists once per class; does not require new to access.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Distinguish between Static and Instance members.

A
  • Static: Shared across all instances; used for utilities or global class state (e.g., User.count).
  • Instance: Unique to each object; used for individual data (e.g., user.email).
  • Constraint: Static methods cannot access instance data via this.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name two advanced TS/JS features of static beyond simple properties.

A
  • Static Blocks: static { ... } for complex, one-time initialization logic.
  • Factory Methods: Static methods that return new instances (e.g., User.fromJSON(data)).
  • Inheritance: Static members are inherited by subclasses via the prototype chain.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When is using static considered a “code smell” or bad practice?

A
  • Overuse as Namespaces: If a method doesn’t use class state, an exported function is better (allows for tree-shaking).
  • Memory Bloat: Static properties live for the app’s entire lifecycle; avoid using them for large caches.

Tip for the Assessment:

If an evaluator asks you to refactor a class full of static methods that don’t share data, suggest moving them to standalone functions.

This is the “functional” approach preferred in modern React/TS environments because it results in smaller bundle sizes.

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

What is a TypeScript Abstract Class and when is it used?

A
  • Definition: A base class that cannot be instantiated directly (new X() fails).
  • Purpose: Defines a contract/template for subclasses to implement.
  • Abstract Methods: Methods marked abstract have no body; subclasses must implement them.
  • Evaluator Tip: Use when multiple classes share some logic but need unique implementations for core steps.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do Abstract Classes differ from Interfaces in TS?

A
  • Interface: Purely a type (erased at runtime); defines structure only.
  • Abstract Class: A real class (exists at runtime); can contain actual code/logic (methods with bodies).
  • Rule: Use an Interface for simple shape definitions; use an Abstract Class if you want to provide shared functionality.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When is an Abstract Class considered “poor” design in modern React?

A
  • Composition over Inheritance: Modern React prefers Hooks and Composition for sharing logic.
  • Bloat: Abstract classes can create deep, rigid hierarchies that are hard to refactor.
  • Verdict: If you just need a type, use an Interface; if you just need shared logic, use a Hook.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the primary technical difference between interface and type?

A
  • Interface: Extendable via Declaration Merging (defining the same name twice merges them). Better for Public APIs and Class Shapes.
  • Type: Can define Unions (string | number), Tuples, and Primitives. Better for Complex Logic and Function Signatures.
  • Performance: interface is slightly faster for the TS compiler to check in large codebases.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Discriminated Union and why is it used in React?

A
  • Definition: A Union type where every object has a common literal property (the “tag”).
  • Example: { type: 'success', data: T } | { type: 'error', error: string }
  • Benefit: Enables Exhaustive Checking in switch statements; prevents accessing properties that don’t exist in a specific state.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do keyof and Record<K, T> do?

A
  • keyof T: Returns a Union of all keys in an object (e.g., "id" | "name").
  • Record<K, T>: Maps keys K to values T.
  • Use Case: Creating a strictly typed Lookup Table or Config object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When is using any or non-null assertion (!) considered a “Fail”?

A
  • any: Disables the compiler; always suggest unknown + Type Guard instead.
  • ! (Non-null): Assumes data exists without checking; leads to runtime “Cannot read property of null.”
  • Fix: Use Optional Chaining (?.) or Nullish Coalescing (??).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly