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:
- Go to nodejs.org
- Download the LTS version (Long Term Support)
- Install with default settings
- Install pnpm (package manager):
npm install -g pnpm
- 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:
- Download from code.visualstudio.com
- Install with default settings
- 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 clientdotenv- Load environment variables from.envtypescript- TypeScript compiler@types/node- Type definitions for Node.jstsx- 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 featuresstrict: true- Enable all type checking (recommended!)outDir: "./dist"- Where compiled JavaScript goesrootDir: "./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:
- Go to platform.openai.com
- Sign up / Log in
- Go to API Keys section
- Create new secret key
- Copy to
.envfile - Add payment method and fund account say $10.00
Weather API Key (Optional):
- Go to openweathermap.org/api
- Sign up for free
- Get API key
- Add to
.envfile
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.jsoncreated -
.envfile with API keys -
.gitignoreincludes.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:
- Explore prompt engineering with more detailed travel examples
- Build on our basic prompt to create a more complex prompt
- Add a system prompt to the prompt
- Create a streaming response from the prompt
- Show how to use structured output from the prompt
- Add a function/tool calling for weather
You're ready for Lesson 3: OpenAI Prompts Overview 🚀
Additional Resources
- TypeScript Reference Guide - Complete TypeScript patterns and examples
- TypeScript Official Docs
- OpenAI API Documentation