Types
1 min readRapid overview
Type Choices in JavaScript/TypeScript
Use these notes to explain when to use types, interfaces, and classes in TS.
Type vs interface
- Type aliases: Great for unions, primitives, and utility types.
- Interfaces: Good for object shapes and extension via declaration merging.
type Status = 'idle' | 'loading' | 'error';
interface User {
id: string;
name: string;
}
Classes
- Use for stateful logic or when you need
instanceofchecks. - Keep constructors simple; move side effects elsewhere.
Generics
- Apply generics to make utilities reusable.
const identity = <T>(value: T): T => value;
Interview prompt
- When do you prefer
typeoverinterfaceand why?