Q44. How should environment variables be typed?

Environment variables are runtime strings and may be missing, even when a declaration file claims otherwise. Define compile-time types for known names if helpful, but parse and validate values at startup. Numbers, booleans, URLs, and secrets should not be blindly trusted as already-correct typed values.

A declaration such as DB_PORT: string does not turn the environment value into a number or guarantee it exists.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";
💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

What is needed beyond a TypeScript declaration for `PORT`?