Welcome to your first-year Software Engineering guide to TypeScript (TS)! This repository is designed specifically for polytechnic students. It focuses on terminal-based execution (no HTML/CSS required) to teach you programming fundamentals, how TypeScript differs from JavaScript, and how to implement classic data structures and algorithms with strong type safety.
- TypeScript vs. JavaScript: The Fundamentals
- Prerequisites & Setup
- Running the Code Examples
- Course Walkthrough & Exercises
- Suggested Lab Curriculum Schedule
- Automated Testing Guide
- Code Quality & Styling (ESLint & Prettier)
- Complexity Reference Guide (Big O)
- Algorithms & Data Structures Pedagogical Notes
- Terminal Mini-Project Guide
TypeScript is a syntactic superset of JavaScript. This means any valid JavaScript code is also valid TypeScript. However, TypeScript adds static typing on top of JavaScript's dynamic type system.
| Feature | JavaScript (JS) | TypeScript (TS) |
|---|---|---|
| Typing Discipline | Dynamic: Variables can hold any type; types are determined and coerced at runtime. | Static: Types are declared and checked when compiling; variables have locked types. |
| Error Detection | Runtime: Type errors (e.g., calling a non-existent function) crash only when the code executes. | Compile-Time: Errors are highlighted in your code editor and stop compilation before execution. |
| Tooling & IDEs | Limited autocompletion because the editor doesn't know the shapes of variables. | Advanced Intellisense, auto-imports, and refactoring because TS knows exact object shapes. |
| Compilation Step | Runs directly in browsers or Node.js without compiling. | Must be compiled (transpiled) into plain JS using the TypeScript Compiler (tsc). |
To run this repository on your computer, you must have Node.js installed.
- Download Node.js: Install the LTS version from nodejs.org.
- Clone / Open the Project: Navigate to this directory in your terminal.
- Install Dependencies:
Run the following command in your terminal to install the TypeScript compiler, testing runner, and styling tools:
npm install
We have pre-configured short terminal commands in package.json to let you execute each chapter's example instantly.
| Command | Description | What it Runs |
|---|---|---|
npm run build |
Compiles TS files to JS | TypeScript compiler (tsc) outputs to dist/ |
npm run run:01-js |
JS Runtime Bug Demo | Executes src/01_ts_vs_js/runtime_error.js using Node |
npm run run:01-ts |
TS Compile-time Protection | Executes src/01_ts_vs_js/compile_error.ts using ts-node |
npm run run:02 |
Basic Types, Enums, and Unions | Executes src/02_basic_types/types.ts using ts-node |
npm run run:03 |
Functions and Type Narrowing | Executes src/03_functions/functions.ts using ts-node |
npm run run:04-stack |
LIFO Stack Data Structure | Executes src/04_data_structures/stack.ts using ts-node |
npm run run:04-list |
Singly Linked List Structure | Executes src/04_data_structures/linked_list.ts using ts-node |
npm run run:05-sort |
Bubble Sort (Numeric & Generic) | Executes src/05_algorithms/bubble_sort.ts using ts-node |
npm run run:05-search |
Binary Search (number | null) |
Executes src/05_algorithms/binary_search.ts using ts-node |
npm run run:06 |
Readline Terminal I/O Demo | Executes src/06_readline/readline_demo.ts using ts-node |
npm start |
Interactive Task Queue CLI | Launches the terminal mini-project menu loop |
npm run test |
Run all unit tests | Executes all vitest test suites in the repository |
npm run test:watch |
Run tests in watch mode | Starts interactive vitest watcher for exercises |
npm run test:ci |
Run CI tests | Runs vitest only on completed chapters to check types |
npm run lint |
Scan code syntax style | Audits all TypeScript files using ESLint checks |
npm run format |
Auto-format files | Standardizes file formats using Prettier styling |
The codebase is organized logically into chapters, configurations, and student exercises:
- package.json: Declares third-party libraries (TypeScript, ts-node, ESLint, Prettier, Vitest) and execution shortcuts.
- tsconfig.json: Standard strict compiler guidelines.
- .eslintrc.json: Syntax style rules designed to ignore template stubs while checking for code safety.
- .prettierrc: Enforces quotes, semicolons, and indentations.
- .github/workflows/ci.yml: Continuous integration pipeline executing compiling, style checks, and tests on push.
- runtime_error.js: Demonstrates silent coercion errors and runtime null pointer crashes in JS.
- compile_error.ts: Shows TypeScript flagging those same bugs during development.
- types.ts: Covers primitive types, arrays, tuples, enums, unions, and
anyvsunknown. - functions.ts: Covers parameters, return typing, defaults, rest operators, and callback types.
- stack.ts: Functional LIFO Stack implementation.
- linked_list.ts: Functional Singly Linked List.
- bubble_sort.ts: Numeric and generic sorting implementation.
-
binary_search.ts: Fast
$O(\log n)$ search returning unions.
- readline_demo.ts: Explains the Node.js native
readlinemodule in TypeScript. Demonstrates:- Callback-based user input.
- Modern async/await promise queries (used in our final project).
- Processing text files line-by-line using input streams (sample.txt).
This directory contains code challenges designed for students to write their own implementations:
- queue.ts: Implement a generic First-In, First-Out (FIFO) Queue.
- linear_search.ts: Implement a generic Linear Search returning
number | null.
For teachers structuring a syllabus, or students studying independently, here is the recommended path through this repository:
graph TD
A["Lab 1: JS vs TS (Ch 1)"] --> B["Lab 2: Basic Types (Ch 2)"]
B --> C["Lab 3: Typing Functions (Ch 3)"]
C --> D["Lab 4: The Stack & LinkedList (Ch 4)"]
D --> E["Lab 5: CHALLENGE - Build a Queue (Exercise)"]
E --> F["Lab 6: Sorting & Search Algorithms (Ch 5)"]
F --> G["Lab 7: CHALLENGE - Linear Search (Exercise)"]
G --> I["Lab 8: Node Readline Terminal I/O (Ch 6)"]
I --> H["Lab 9: Final Project (CLI Task Manager)"]
-
Lab 1: Understanding Compile-time Safety
- Files: runtime_error.js and compile_error.ts
-
Activity: Run
npm run run:01-jsand observe the crash. Opencompile_error.tsand inspect how TS catches this before running. Runnpm run run:01-ts.
-
Lab 2: Declaring Data Types
- Files: types.ts
-
Activity: Read through the file and run
npm run run:02. Notice the difference betweenanyandunknowntype safety.
-
Lab 3: Input & Output Contracts
- Files: functions.ts
-
Activity: Study how function parameters, return types, optional/default values, and callback signatures are typed. Run
npm run run:03.
-
Lab 4: Building Data Structures
- Files: stack.ts and linked_list.ts
-
Activity: Study the generic implementations. Run them using
npm run run:04-stackandnpm run run:04-listto trace how elements move.
-
Lab 5: Practice Exercise — FIFO Queue
- Files: queue.ts (Test suite: queue.test.ts)
-
Activity: Complete the Queue implementation
TODOstubs. Runnpm run testornpm run test:watchto verify your solution against the unit tests.
-
Lab 6: Complex Algorithms
- Files: bubble_sort.ts and binary_search.ts
-
Activity: Study how Bubble Sort utilizes comparators and how Binary Search operates in
$O(\log n)$ time. Runnpm run run:05-sortandnpm run run:05-search.
-
Lab 7: Practice Exercise — Linear Search
- Files: linear_search.ts (Test suite: linear_search.test.ts)
-
Activity: Open the exercise file, implement the linear search logic, and run
npm run testto check if your code passes the assertions.
-
Lab 8: Input/Output and Streams
- Files: readline_demo.ts (Data source: sample.txt)
-
Activity: Run
npm run run:06and interact with the terminal inputs. Examinereadline_demo.tsto see how streams and async processes work.
-
Lab 9: The Terminal Project
- Files: index.ts (CLI Menu), task_manager.ts (Logic), and types.ts (Models)
-
Activity: Run
npm startto interact with the Task Queue. Review the codebase to see how it ties all the semester's concepts (data structures, sorting, binary search, readline, interfaces) together.
We use Vitest for running unit tests. The tests check if code is implemented correctly.
To run all test suites once (great for checking if your exercises are correct):
npm run test(Initially, the Queue and Linear Search tests will fail. Your goal is to write code in src/exercises/ to make them pass!)
To start an interactive runner that automatically re-runs tests every time you save a file:
npm run test:watchIn software engineering, teams use automated tools to ensure consistent code styling and code safety.
To run ESLint and scan your code for potential bugs, unused variables, or bad styling practices:
npm run lintTo automatically format all your files (fixing indentation, quotes, semi-colons):
npm run formatTo learn about computational efficiency, time complexity, and space complexity, read the dedicated guide:
-
Big O Complexity Reference Guide: Explains constant
$O(1)$ , logarithmic$O(\log n)$ , linear$O(n)$ , and quadratic$O(n^2)$ costs, comparing our implemented structures.
Without TypeScript generics, writing a stack or linked list in JavaScript means it can accept any data type, meaning you could push a number, then a string, then a boolean, leading to bugs. In TypeScript, declaring createStack<string>() tells the compiler to only allow strings to be pushed, protecting your queue from type mismatches.
In many languages (including JavaScript), accessing elements on a null reference causes sudden crashes. TypeScript enforces strict null checks. When traversing a linked list node (node.next), TypeScript forces you to check if the next node is null before accessing node.next.value.
The culminating project is located in src/project/. It is an interactive, menu-driven command-line Task Queue & Priority Processor.
- types.ts: Declares the
Taskinterface, theTaskPriorityenum, and theTaskQueuetype. - task_manager.ts: Contains pure/utility functions for:
- Enqueuing tasks (
addTask). - Sorting tasks by priority (High -> Medium -> Low) using custom Bubble Sort.
- Sorting tasks by ID and using Binary Search to look up tasks.
- Dequeuing tasks (
processNextTask).
- Enqueuing tasks (
- index.ts: Standard CLI input-output loop.
Start the program using:
npm startYou will be prompted with a menu to add tasks, sort them by priority level, search for tasks by ID, process them, and print the queue status directly to the terminal!