2.7 TypeScript Playground: Exploring Without a Local Setup

Before you go downloading anything, let’s get one thing straight: you don’t need a local setup to start playing with TypeScript. The TypeScript team, in a moment of pure genius and empathy, built the TypeScript Playground. Think of it as a digital sandbox where you can build castles of code without getting sand in your laptop’s keyboard. It’s the perfect place to experiment, share ideas, and, most importantly, prove to a colleague that your type definition is superior to theirs, all without the ceremony of npm install.

2.6 The Compilation Output: What tsc Produces

Right, so you’ve run tsc on your index.ts file. You saw it flash by in the terminal, and now you have a new index.js file sitting there. It feels a bit like magic, but we’re not here for magic tricks. We’re here to understand the engineering. Let’s crack open that output file and see what the compiler actually did for us. This is where you go from “it just works” to knowing why it works.

2.5 Your First TypeScript Program: Types Alongside JavaScript

Right, let’s get our hands dirty. We’ve set up the machinery; now let’s see what it actually does. The core premise of TypeScript is simple: it’s JavaScript with a type system slapped on top. But don’t let that simplicity fool you—this is where the magic, and more importantly, the sanity, happens. You already know how to write this in JavaScript: // plain-old-javascript.js function greet(name) { return "Hello, " + name; } console.log(greet("Alice")); // "Hello, Alice" console.log(greet(42)); // "Hello, 42" ... wait, what? The JavaScript version is perfectly happy, if a bit naive. It will cheerfully concatenate a number onto a string, which might not be what you intended. It’s like a friendly golden retriever that brings you a shoe instead of a ball—the enthusiasm is there, but the execution is flawed.

2.4 tsconfig.json: Generating and Understanding the Defaults

Right, so you’ve got TypeScript installed. You can run tsc --version and feel a brief moment of power. Now you’re staring at an empty project directory. You might be tempted to just run tsc main.ts and call it a day. Don’t. That’s like using a particle accelerator to crack a walnut. It works, but you’re missing the point and you’ll eventually create a black hole that swallows your project whole. The proper way to wield this tool is with a tsconfig.json file. This is your project’s mission control, and we’re going to build it from the ground up.

2.3 ts-node and tsx: Running TypeScript Directly

Alright, let’s get one thing straight: compiling your TypeScript with tsc every single time you want to run a tiny script is a fantastic way to lose your will to live. It’s the programming equivalent of having to assemble a car engine just to drive to the mailbox. You’re a developer, not a build-system masochist. This is where ts-node and tsx come in—they’re your power tools for skipping the “generate a .js file” middleman and running your beautiful TypeScript code directly.

2.2 The tsc Command: Compiling a Single File

Alright, let’s get our hands dirty. You’ve written a beautiful piece of TypeScript code in a file called greeter.ts. It’s pristine. It’s elegant. It has type annotations. Your browser, however, couldn’t care less about type annotations. It speaks JavaScript, and frankly, it finds your sophisticated types a bit pretentious. This is where tsc, the TypeScript compiler, comes in. Its job is to be the translator, converting your lofty TypeScript (greeter.ts) into the plain, workmanlike JavaScript (greeter.js) that the browser or Node.js can actually execute.

2.1 Installing TypeScript Globally and Per-Project with npm

Alright, let’s get you set up to actually use TypeScript. We’re going to do this the right way, which means I’m going to talk you out of installing it globally almost immediately. Stick with me. The first thing you need is Node.js and its trusty sidekick, npm (or its speedier, modern cousin, pnpm or yarn). If you don’t have Node.js on your machine, go grab the LTS version from the official website. I’ll wait. Don’t worry, this isn’t the kind of book where I make you feel bad for not having things installed beforehand. We’ve all been there.

— joke —

...