Type something to search...
TypeScript 101: Putting ‘seatbelts’ on JavaScript

TypeScript 101: Putting ‘seatbelts’ on JavaScript

TypeScript is JavaScript with static types: it catches type errors in your editor or at build time, before the code ever runs. For anything beyond a short hackathon or a very light script, adopting it is worth the small upfront cost of writing types.

Why does JavaScript's flexibility cause bugs?

JavaScript is the most widely used language in the world, and it is flexible and easy to write. But as a project grows larger and more complex, that 'flexibility' often becomes a serious liability.

function addNumbers(a, b) {
  return a + b;
}

// Developer's intent: add the numbers 1 and 2 and expect 3
console.log(addNumbers(1, 2)); // 3

// When a string is entered by mistake
console.log(addNumbers("1", "2")); // "12"

In the code above, JavaScript does not raise an error. It simply concatenates the strings "1" and "2" and returns the odd result "12". The mistake is discovered only at runtime, and if you are unlucky, it shows up as an error on the user's screen after the service is deployed.

TypeScript is a language developed by Microsoft to overcome these limitations of dynamically typed languages and catch bugs 'before executing' the code.

What is TypeScript?

TypeScript, as its name suggests, is "JavaScript with types added." Officially, it is a Superset of JavaScript: it supports 100% of existing JavaScript syntax and adds Static Type Checking on top of it.

Browsers and Node.js cannot read TypeScript code directly, so TypeScript is translated into plain JavaScript by a compiler (TSC) before it runs.

What are the benefits of using TypeScript?

  1. Error detection before execution (stability): Type mismatches are flagged with a red line as you type in an editor (VS Code, etc.) or during the build. This greatly reduces the risk of "Runtime Errors".
  2. Powerful auto-completion (development productivity): IntelliSense (auto-completion) works very well because the editor knows exactly what properties an object has, what argument types a function accepts, and what it returns.
  3. Code that documents itself (collaboration): When using a complex function someone else wrote, you can understand how to use it from its declared types without reading all of its internal logic.

What does core TypeScript syntax look like?

1. Specify basic type

Specify a type by adding a colon (:) after a variable name or argument.

// Primitive Types
let userName: string = "Jules";
let age: number = 30;
let isDeveloper: boolean = true;

// Array
let skills: string[] = ["React", "TypeScript", "Node.js"];

// Let's buckle up the risky function from the example above.
function addNumbers(a: number, b: number): number {
  return a + b;
}

// Now the editor draws a red line and blocks execution!
// Error: Argument of type 'string' is not assignable to parameter of type 'number'.
addNumbers("1", "2");

2. Definition of object type (Interface & Type Alias)

One of the most confusing parts of JavaScript development is figuring out what data an object contains. In TS, you can design an object's shape in advance with the interface or type keyword.

// Define the type of object called User.
interface User {
  id: number;
  name: string;
  email: string;
  optionalProfileImage?: string; // If you add ?, it becomes an optional attribute that 'may or may not be present'.
}

// Give the variable the type created above.
const myProfile: User = {
  id: 1,
  name: "Jules",
  email: "[email protected]"
  // There is no error even if optionalProfileImage is omitted.
};

// The moment you type myProfile. , the id, name, and email properties are auto-completed!

3. Union Types

Union types allow some of JavaScript's flexibility by declaring that a value "can be of type A or B".

// id can be a number or a unique string.
function printId(id: number | string) {
  console.log(`Your ID is: ${id}`);
}

printId(101); // OK
printId("UUID-1234"); // OK
printId(true); // Error: boolean not allowed

Should you adopt TypeScript?

In conclusion, "unless it is a short-term hackathon or a very light script, it is definitely beneficial to introduce it."

At first, specifying a type for each variable can slow down coding and feel cumbersome. But as a project grows, the bugs TypeScript catches in advance and its powerful auto-completion repay that initial cost several times over. As of 2024, TypeScript is no longer optional but a 'basic skill' in the front-end and back-end (Node.js) ecosystem.

Related Post

AI-Native Development: The End of Traditional Coding as We Knew It

AI-Native Development: The End of Traditional Coding as We Knew It

AI-Native Development Platforms go beyond autocomplete: they treat the AI as a core member of the engineering team that understands your whole repository and can propose architecture, generate se

The Prototype Economy: How AI Makes a Fully Working App Possible in 48 Hours

The Prototype Economy: How AI Makes a Fully Working App Possible in 48 Hours

The Prototype Economy is the 2026 reality where AI coding assistants, agentic workflows, and generative design tools shrink the gap between an idea and a clickable prototype from months to minute

Down the Rabbit Hole: Building Your First Custom Mechanical Keyboard

Down the Rabbit Hole: Building Your First Custom Mechanical Keyboard

Building a custom mechanical keyboard from scratch comes down to four stages: choosing a barebones kit, picking and lubing switches, tuning the stabilizers, and fitting keycaps. It is slow, fiddly wo

Self-Hosting a Developer's NAS: Replacing Paid Cloud Storage

Self-Hosting a Developer's NAS: Replacing Paid Cloud Storage

A custom NAS can do far more than store files: for a developer, it can replace paid cloud storage, private Git hosting, and on-demand CI servers with a single box at home. The trade-off is that you b

Can a Dual-Screen Laptop Replace a Multi-Monitor Desktop? A 2026 Developer Review

Can a Dual-Screen Laptop Replace a Multi-Monitor Desktop? A 2026 Developer Review

A dual-screen laptop can stand in for a multi-monitor desktop, but only for certain people. It shines for hybrid workers and digital nomads who need a full workspace on the move; for someone who sits

Enterprise Agentic AI: From Prompting to Delegating in 2026

Enterprise Agentic AI: From Prompting to Delegating in 2026

Enterprise agentic AI means handing whole workflows to coordinated teams of AI agents instead of prompting a single assistant line by line. In 2026 the shift is less about better autocomplete and mor

Feeling the Digital World: Haptic VR Gloves for Remote Work

Feeling the Digital World: Haptic VR Gloves for Remote Work

Consumer-grade haptic VR gloves add the missing sense of touch to spatial computing: virtual buttons push back, digital objects resist your grip, and textures can be felt through micro-vibrations. As

Migrating from React to HTMX: Is It Worth It? (A Practical Review)

Migrating from React to HTMX: Is It Worth It? (A Practical Review)

Moving a React back-office to HTMX is worth it for data-heavy admin screens: frontend code shrinks dramatically, backend developers can ship UI directly, and the HTMX library itself is only about

The Golden Age of Solo Dev: Building Games in Your Bedroom with AI

The Golden Age of Solo Dev: Building Games in Your Bedroom with AI

In 2026, a single person can realistically build a polished game from their bedroom. Visual scripting in modern game engines removes much of the coding barrier, generative AI covers art, music and de

Can the 2026 iPad Pro Actually Replace a MacBook for Developers? A Practical Breakdown

Can the 2026 iPad Pro Actually Replace a MacBook for Developers? A Practical Breakdown

No, the 2026 M4 iPad Pro cannot fully replace a MacBook for a professional developer. It works remarkably well as a thin client for remote development over SSH, but the lack of local Docker, a na

Coding with OpenAI's o1 Model: Strengths, Limits and Costs

Coding with OpenAI's o1 Model: Strengths, Limits and Costs

OpenAI's o1 model, launched in 2024, is a reasoning model: instead of answering as fast as possible like GPT-4o, it works through a "Chain of Thought" before replying. For coding, that makes it s

The Complete Guide to Docker: Introduction to and Use of Container Technology for Beginners

The Complete Guide to Docker: Introduction to and Use of Container Technology for Beginners

Docker is a platform that packages an application and everything it needs into a standardized, portable unit called a container, so it runs the same way on any machine. Containers share the host oper

Mastering Kubernetes: Container Orchestration Beyond Docker

Mastering Kubernetes: Container Orchestration Beyond Docker

Kubernetes (k8s) is a container orchestration tool that automates deploying, scaling, and managing dozens or hundreds of containers across many servers. Where Docker packages a single application, Ku

The Complete Guide to Git Branching Strategy: From Git Flow to GitHub Flow

The Complete Guide to Git Branching Strategy: From Git Flow to GitHub Flow

Why does a team need a Git branching strategy? A shared branching strategy keeps a multi-developer repository from descending into chaos. The three most widely used are Git Flow (structured, for

React vs Vue.js: Guide to Choosing a Front-End Framework in 2024

React vs Vue.js: Guide to Choosing a Front-End Framework in 2024

React or Vue.js: which should you choose? Choose React for large, long-lived projects, mobile apps via React Native, and the biggest job market and ecosystem; choose Vue.js for fast prototypes, m

Website Performance Optimization Strategies: How Loading Speed ​​​​Affects Your Business

Website Performance Optimization Strategies: How Loading Speed ​​​​Affects Your Business

Loading speed directly affects revenue: even a fraction of a second of delay can lower sales and push visitors away. The most effective front-end fixes are optimizing images, shrinking text resources

Python Data Analysis Basics: Mastering Pandas Core Functions

Python Data Analysis Basics: Mastering Pandas Core Functions

Pandas is the core Python library for manipulating and analyzing tabular data, much like Excel spreadsheets or SQL tables. To get started, you need two data structures, Series and DataFrame, plus

Complete CI/CD pipeline automation starting with GitHub Actions

Complete CI/CD pipeline automation starting with GitHub Actions

CI/CD replaces error-prone manual deployment with automated builds, tests, and releases, and GitHub Actions is one of the easiest ways to set it up because it runs directly inside your GitHub reposit

Practical guide to developer-prompted engineering in the era of generative AI

Practical guide to developer-prompted engineering in the era of generative AI

Prompt engineering for developers means structuring instructions so that generative AI (ChatGPT, Claude, etc.) produces code you can actually use: give the AI a role, explain the project context, sta

Front-end ecosystem trends in 2024: What should we learn and prepare for?

Front-end ecosystem trends in 2024: What should we learn and prepare for?

The front-end development trend in 2024 can be summarized as a 'return to server-side rendering' and 'extreme performance optimization'. The four trends to learn and prepare for are React Ser

Next.js 15+ Deep Dive: Server Component Optimization and Rendering Architecture Evolution

Next.js 15+ Deep Dive: Server Component Optimization and Rendering Architecture Evolution

The core of Next.js 15+ performance is React Server Components (RSC) on the App Router: let server components do most of the heavy rendering, push client components to the leaves of the tree, and com