TypeScript Fundamentals - Types & Type System
The Production Bug That Taught Me Type Safety
interface Config {
port: number;
timeout: number;
retries: number;
}
function startServer(config: Config) {
server.listen(config.port);
http.setTimeout(config.timeout);
http.setMaxRetries(config.retries);
}
// Load from environment
const config: Config = {
port: process.env.PORT, // β Looks fine to TypeScript!
timeout: process.env.TIMEOUT,
retries: process.env.RETRIES
};
startServer(config);Basic Types
String
Number
Boolean
Null and Undefined
Any
Unknown
Void
Never
Arrays
Array Type Syntax
Array Methods Preserve Types
ReadonlyArray
Tuples
Named Tuples (TypeScript 4.0+)
Type Annotations vs Type Inference
Type Inference
When to Use Explicit Annotations
Literal Types
Combining Literals
Type Assertions
Real-World Example: Type-Safe Configuration
Before (Unsafe)
After (Type-Safe)
Your Challenge
Key Takeaways
What I Learned From the Environment Variable Bug
Last updated