Week 1, Lesson 2: TypeScript Setup & Basics

TypeScript setup and basics for AI development.

Published: 1/1/2026

Why TypeScript for AI Development?

TypeScript is JavaScript with superpowers. It adds type safety, catching errors before your code runs and making development faster and safer.

The Problem with Plain JavaScript

// JavaScript - no safety
async function bookFlight(destination, passengers) {
  // What if someone passes wrong types?
  return await api.book(destination, passengers);
}

bookFlight(123, "Paris"); // Backwards! But JavaScript doesn't care
// Error only appears when the code runs 💥

The TypeScript Solution

// TypeScript - catches errors immediately
async function bookFlight(
  destination: string,
  passengers: number
): Promise<Booking> {
  return await api.book(destination, passengers);
}

bookFlight(123, "Paris");
// ❌ Error: Argument of type 'number' is not assignable to parameter of type 'string'
// Your editor shows this BEFORE you run the code! ✅

Complete Setup Guide

Step 1: Install Node.js and pnpm

TypeScript runs on Node.js, so install it first:

  1. Go to nodejs.org
  2. Download the LTS version (Long Term Support)
  3. Install with default settings
  4. Install pnpm (package manager):
npm install -g pnpm
  1. Verify installation:
node --version  # Should show v24.x.x or higher
pnpm --version  # Should show 10.x.x or higher

Step 2: Install VS Code

I'm using VS Code for this course, but you can use any editor you want:

  1. Download from code.visualstudio.com
  2. Install with default settings
  3. VS Code has built-in TypeScript support!

Recommended Extensions:

  • "TypeScript Importer" - Auto-imports
  • "Error Lens" - Shows errors inline
  • "ESLint" - Code quality

Step 3: Create Your Project

# Create project folder
mkdir ai-travel-assistant
cd ai-travel-assistant

# Initialize Node.js project
pnpm init

# Install dependencies
pnpm add openai dotenv

# Install TypeScript dependencies
pnpm add -D typescript @types/node tsx

What each package does:

  • openai - OpenAI API client
  • dotenv - Load environment variables from .env
  • typescript - TypeScript compiler
  • @types/node - Type definitions for Node.js
  • tsx - Run TypeScript files directly (no compilation step)

Using tsx directly: You can run TypeScript files without setting up npm scripts by using tsx directly:

tsx src/test-setup.ts        # Run a file once
tsx watch src/index.ts        # Run with auto-reload on file changes

Step 4: Configure TypeScript

Create tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020", // OK - Modern JS output (good for most Node.js LTS)
    "module": "commonjs", // OK - Works for most Node.js setups (switch to "ESNext" if using ESM)
    "outDir": "./dist", // OK - Compiled JS goes here
    "rootDir": "./src", // OK - Your TS code lives here
    "strict": true, // OK - Strong type safety (recommended)
    "esModuleInterop": true, // OK - Smoother `import`/`require` mixing
    "skipLibCheck": true, // OK - Faster builds, less error noise
    "forceConsistentCasingInFileNames": true // OK - Platform-agnostic builds
  },
  "include": ["src/**/*"], // OK - Compiles your source code only
  "exclude": ["node_modules", "dist"] // OK - Ignores deps & build output
}

Key settings explained:

  • target: "ES2020" - Use modern JavaScript features
  • strict: true - Enable all type checking (recommended!)
  • outDir: "./dist" - Where compiled JavaScript goes
  • rootDir: "./src" - Where your TypeScript code lives

Step 5: Set Up pnpm Scripts

Edit package.json and add:

{
  "scripts": {
    "dev": "tsx src/index.ts",
    "build": "tsc",
    "start": "node dist/index.js"
  }
}

How to use:

pnpm dev       # Run TypeScript directly (development)
pnpm build     # Compile to JavaScript
pnpm start     # Run compiled JavaScript (production)

Step 6: Create Environment File

Create .env:

OPENAI_API_KEY=sk-your-key-here
WEATHER_API_KEY=your-weather-key-here

Create .gitignore:

node_modules/
dist/
.env
*.log

Step 7: Get API Keys

OpenAI API Key:

  1. Go to platform.openai.com
  2. Sign up / Log in
  3. Go to API Keys section
  4. Create new secret key
  5. Copy to .env file
  6. Add payment method and fund account say $10.00

Weather API Key (Optional):

  1. Go to openweathermap.org/api
  2. Sign up for free
  3. Get API key
  4. Add to .env file

Free tier: 60 calls/minute, 1,000,000 calls/month


TypeScript Basics for AI Development

For a comprehensive TypeScript reference including types, interfaces, error handling, and common patterns, see the TypeScript Reference Guide.

Key Concepts to Review

  • Basic Types: strings, numbers, booleans, arrays
  • Function Types: typed parameters and return values
  • Interfaces: defining object shapes
  • Error Handling: type guards and proper error checking
  • Generic Types: flexible, reusable code

The reference guide includes all the TypeScript patterns you'll use throughout this course with detailed examples and explanations.


Checklist: Setup Complete

Before moving on, verify:

  • Node.js installed (v24+)
  • VS Code or any other code editor installed
  • Project created with pnpm init
  • Dependencies installed
  • tsconfig.json created
  • .env file with API keys
  • .gitignore includes .env
  • Test file runs successfully (tsx src/basic-prompt.ts)
  • Understand basic TypeScript types
  • Comfortable with interfaces
  • Know how to handle errors with types

What's Next?

Now that we have TypeScript set up, we'll:

  1. Explore prompt engineering with more detailed travel examples
  2. Build on our basic prompt to create a more complex prompt
  3. Add a system prompt to the prompt
  4. Create a streaming response from the prompt
  5. Show how to use structured output from the prompt
  6. Add a function/tool calling for weather

You're ready for Lesson 3: OpenAI Prompts Overview 🚀


Additional Resources