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.