Jiaxi Liu (Jesse)

Master’s Graduate

Software Engineer | Scalable APIs · Web Scraping · Data Integration · Code Quality & Refactoring

Back to Blog

TypeScript Foundations: Types, Compilation, and Variable Declarations

TypeScript helps move many JavaScript mistakes from runtime to development time. JavaScript is flexible, but that flexibility can hide problems until the code is executed: unexpected value types, missing object properties, or invalid function arguments.

Compilation and Type Checking

TypeScript is usually compiled into JavaScript before it runs. During this process, the compiler checks whether types are being used consistently.

The important point is that TypeScript types mostly exist during development and build time. The runtime output is still JavaScript.

Variable Declarations

Modern TypeScript and JavaScript should prefer let and const.

let count: number = 10;
count = 20;
 
const siteName: string = "Portfolio";

var has function scope and can cause hoisting and scope pollution issues.

if (true) {
  var message = "hello";
}
 
console.log(message); // still accessible

let and const have block scope.

if (true) {
  let message = "hello";
}
 
// console.log(message); // compile error

Type Inference

TypeScript can infer many types from initial values.

let name = "Alice"; // string
let age = 30; // number

A useful rule: rely on inference for simple local variables, but annotate function parameters, return values, and complex object structures.

A Detail About const

const prevents reassignment of the binding. It does not make object contents immutable.

const person = { name: "Alice", age: 25 };
person.age = 26;
 
// person = {}; // not allowed

This distinction helps separate immutable references from immutable data.

Deeper Notes

When reviewing this topic, do not memorize names only. Focus on TypeScript compilation, type checking, variable declarations, inference, and the real meaning of const. If this stays at the definition level, it becomes hard to explain in interviews or apply in projects. A stronger way to study it is to place it in a concrete scenario: who calls it, where the input comes from, what happens on failure, and whether data or state can be processed twice.

  • TypeScript moves uncertainty from runtime to development time, but it does not replace runtime validation.
  • Type design should model business constraints first instead of showing off complex generics.
  • For third-party input, JSON, forms, and API responses, combine types with narrowing or schema validation.

In a real project, use it as a decision framework: identify inputs, constraints, failure modes, and observability before choosing a specific tool or pattern. If a solution looks simple, keep asking whether it still works when scale grows, permissions change, recovery matters, and more people collaborate on it.

Practical Checklist

  • Identify where this concept sits in the system: development-time constraint, runtime behavior, infrastructure capability, or collaboration workflow.
  • Write one minimal working example and one failure example; only knowing the happy path is usually not enough.
  • Record common misuses: edge cases, permission assumptions, performance assumptions, sync/async differences, or environment differences.
  • Connect the concept to a project experience so that an interview answer can be grounded in real tradeoffs.
  • End with one sentence about tradeoff: what it gives up and what it buys.

Self-Check Questions

  1. What core problem does this topic solve?
  2. What alternatives exist, and what are their costs?
  3. Where are the most likely edge cases?
  4. How would code, tests, or monitoring prove that it is reliable?

Applied Scenario

A practical scenario is a project with shared frontend/backend types. An API returns JSON; the frontend validates it with a schema, then passes the validated data into TypeScript types. Types improve editor feedback and compile-time checking, but they cannot guarantee that network data is valid. Good type design expresses business constraints such as roles, states, permissions, request parameters, and response shapes instead of turning everything into string or any.

Common Pitfalls:

  • Treating TypeScript types as runtime validation.
  • Using any for convenience and pushing errors into production.
  • Making generics so complex that callers cannot understand them.