Typescript written over the brand colors associated with it
Create a Typescript testing sandbox
March 25th, 2022

Exploring TypeScript: A Guide to Getting Started

TypeScript (TS) is a powerful superset of JavaScript (JS) that offers developers the benefits of a statically typed language while maintaining compatibility with JavaScript. In this article, we will dive into the world of TypeScript, explore its key differences from JavaScript, and guide you through setting up a TypeScript project for seamless development.

Understanding TypeScript

At its core, TypeScript is a statically typed language, which means you declare variable types before using them. This provides a level of type safety that JavaScript lacks. Let's illustrate this with a simple example:


let value = 19;

value = "hello there"; // No error in JavaScript
value = true; // Still no error

In JavaScript, you can assign variables different types without any issues, which can lead to subtle bugs.


let value: number = 19;

// Error: Type 'string' is not assignable to type 'number'.
value = "hello there";

TypeScript, on the other hand, will catch type-related errors during development. This feature makes TypeScript a powerful tool for preventing common programming mistakes and enhancing code quality.

It's important to note that TypeScript is a "compile-time" tool, meaning it helps developers during the development phase but compiles down to JavaScript for execution. Browsers cannot run TypeScript natively yet.

If you're looking for more in-depth explanations, consider watching these insightful videos:

Getting Started with TypeScript

To embark on your TypeScript journey, you'll need a few essential tools:

  • Node.js
  • A package manager like npm, Yarn, or pnpm
  • A code editor (e.g., VSCode, Neovim, Sublime Text, etc.)

Creating a TypeScript Project

Start by creating a new project directory. Let's name it "ts-testing."

To initialize a package.json file, run the following command, depending on your package manager of choice:

# Using npm
npm init

# Using Yarn
yarn init

# Using pnpm
pnpm init

Installing Dependencies

Now, create a TypeScript file (e.g., main.ts). To set up your TypeScript environment for Node.js, install the following dependencies:

# Using npm
npm install -D typescript ts-node ts-node-dev @types/node

# Using Yarn
yarn add -D typescript ts-node ts-node-dev @types/node

# Using pnpm
pnpm add -D typescript ts-node ts-node-dev @types/node

Here's a quick overview of these dependencies:

  • typescript: Provides TypeScript support.
  • ts-node: Allows you to run TypeScript files in Node.js.
  • ts-node-dev: Automatically restarts the TypeScript compiler when files change.
  • @types/node: Offers TypeScript type definitions for Node.js.

Your package.json should now look something like this:

  "name": "ts-testing",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {},
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^17.0.35",
    "ts-node": "^10.8.0",
    "ts-node-dev": "^1.1.8",
    "typescript": "^4.7.2"

Your project directory structure will resemble the following:

├── node_modules/
├── main.ts
├── package.json
├── pnpm-lock.yaml

(Note: If you used a different package manager, your lock file may be named yarn.lock or package-lock.json.)

Final Steps

To simplify your development process, add the following scripts to your package.json:

  "name": "ts-testing",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "ts-node main.ts",
    "dev": "ts-node-dev --respawn main.ts"
  // ...rest of the file...

With these scripts in place, running pnpm dev, yarn dev, or npm run dev will execute your main.ts file. If you don't see any output, don't worry—it's because we haven't written any code yet.

To test if everything is set up correctly, add the following line to your main.ts:

console.log("Hello, TypeScript!");

Now, when you run your development script, it should output "Hello, TypeScript!" to the console. Congratulations! Your TypeScript sandbox is ready, and you can begin your TypeScript journey.

In this article, we've explored TypeScript's fundamentals, set up a development environment, and laid the groundwork for your TypeScript projects. Happy coding! 🚀

Whether you're an experienced developer or just starting, TypeScript's static type checking and enhanced tooling can improve your JavaScript projects. So why not give it a try in your next project?