TypeScript Setup and Environment
Setting up a proper TypeScript development environment is crucial for a productive workflow. This guide will walk you through the process of installing TypeScript, configuring your development environment, and preparing the essential tools for TypeScript development.
Prerequisites
Before setting up TypeScript, ensure you have the following:
- Node.js and npm: TypeScript compiler runs on Node.js
- A code editor: Visual Studio Code is recommended for this course
- Basic familiarity with command-line operations
Installing TypeScript
A global installation allows you to use the TypeScript compiler tsc
from any directory on your system. You can either install it through npm globally, or if you are using homebrew on a MacOS you can install it through homebrew.
After you've installed if, you can check the TypeScript version installed on your machine with the help of tsc --version
.
You should see output similar to: Version 5.3.3
(or whatever version is currently installed).
using npm install
using homebrew
check ts version
Project-Based Installation (Recommended)
For most projects, it's better to install TypeScript locally:
With a local installation, you can run the TypeScript compiler using npx
:
This approach has several advantages:
- Version consistency: Everyone working on the project uses the same TypeScript version
- Project isolation: Different projects can use different TypeScript versions
- Better CI/CD integration: Continuous integration pipelines will automatically use the correct version
Setting Up Your First TypeScript Project
Let's set up a basic TypeScript project from scratch:
1. Create a Project Structure
To create a new TypeScript project we will need to have some familiarity with the terminal.
2. Initialize TypeScript Configuration
Create a TypeScript configuration file tsconfig.json
:
This generates a tsconfig.json
file with default settings and helpful comments.
3. Create a Simple TypeScript File
Create a source directory and your first TypeScript file:
Create a file at src/index.ts
with the following content:
src/index.ts
4. Compile Your TypeScript Code
Compile your TypeScript code to JavaScript:
By default, this will look for the tsconfig.json
file and compile according to the settings defined there.
5. Run the Compiled JavaScript
You should see: Hello, TypeScript!
Configuring TypeScript with tsconfig.json
The tsconfig.json
file controls the TypeScript compiler's behavior. Let's explore some important configuration options:
Essential Configuration Properties
Here's a basic tsconfig.json file with commonly used options:
basic tsconfig.json
Commonly Used Compiler Options
Target JavaScript Version
The target
option specifies which JavaScript version the TypeScript compiler should output:
Choose a target based on your deployment environment:
- For modern browsers:
es2016
or newer - For older browsers support:
es5
- For Node.js: Match your Node.js version (e.g., Node.js 14+ supports
es2020
)
Module System
The module
option determines which module system the output JavaScript uses:
Choose based on your runtime environment:
- For Node.js:
commonjs
- For modern browsers or bundlers (webpack, Rollup):
es2015
orESNext
Type Checking Strictness
TypeScript offers various levels of type checking strictness:
For beginners, you might start with "strict": false
and gradually enable stricter checks as you become more comfortable with TypeScript. But usually in React Web-App development we use the "strict": true
so that we can create more robust webapps.
Source Maps
Source maps help with debugging by mapping compiled JavaScript back to the original TypeScript code:
Additional Important Options
Here are some more optional but important options you might want to include in your project:
Setting Up an IDE for TypeScript Development
While TypeScript works with any text editor, using an integrated development environment (IDE) with TypeScript support will significantly improve your productivity.
Visual Studio Code (Recommended)
Visual Studio Code (VS Code) is what we'll use in this TypeScript development course. Of course you can use other IDE's if you want.
1. TypeScript Support: VS Code has built-in TypeScript support, including:
- Syntax highlighting
- IntelliSense (code completion)
- Error checking
- Quick fixes and refactoring
2. Useful Extensions for TypeScript Development:
- ESLint dbaeumer.vscode-eslint: Integrates ESLint into VS Code
- Prettier esbenp.prettier-vscode: Code formatter
- Path Intellisense christian-kohler.path-intellisense: Autocompletes filenames
- Error Lens Alexander.errorlens: Enhances error visibility
- BiomeJS biomejs.biomejs: A toolchain of the web. This is my personal favourite and I use it on my personal projects instead of ESLint and Prettier, since it comes out of the box with these functionalities as well it's stricter and you can opt out of rules.
Setting Up a TypeScript Development Workflow
A robust TypeScript workflow typically includes the following components:
1. Compilation and Watching
For automatic recompilation when files change. This is what you'll usually use when developing with TypeScript, because it compiles the codebase whenever you save a file.
watch for code changes
To enhance it even further, you can add the following commans to your package.json
:
package.json
And then you can simply run it by typing the command npm run watch
in your terminal.
Best Practices for TypeScript Project Setup
1. Structuring Your Project
In all the courses we will go through, a good project structure improves maintainability and is one of the most important things while creating a project.
Example project structure
2. Managing Types
Keep your types organized:
- Place types close to their usage: For component-specific types, define them in the same file
- Create a dedicated types directory: For shared types across the application
- Use namespaces or prefixes: For related groups of types (e.g., Api.Response,
User.Settings
) - Export types from a central location: Create barrel exports for easy imports
Example of a barrel file src/types/index.ts
:
3. Using Declaration Files
Declaration files \*.d.ts
let you add type information to existing JavaScript libraries or define global types:
global.d.ts
global.d.tsx
module-augmentation.d.ts:
module-augmentation.d.ts
TypeScript Fundamentals Exercise
It's time for a simple exercise!
Overview
In this exercise, students will create a basic command-line inventory management system for a bookstore. This will help reinforce TypeScript fundamentals without requiring any frameworks or complex setup. Try to solve this without the solution collapsed.
Exercise Instructions
1. Project Setup
- Create a new directory for the project named
bookstore-inventory
- Initialize a new npm project
- Install TypeScript locally to the project
- Create a basic
tsconfig.json
file - Set up the project structure with a
src
folder
2. Creating the Data Types
Create a file called types.ts
in the src
folder and define the following types:
-
Book
interface with properties:- id (string)
- title (string)
- author (string)
- year (number)
- genre (string literal type with options: "fiction", "non-fiction", "science", "fantasy")
- price (number)
- inStock (boolean)
-
Inventory
type representing an array ofBook
objects
3. Implementing the Inventory Management Features
Create a file called inventory.ts
in the src
folder and implement the following functions:
addBook
: Add a new book to the inventory (with proper type validation)removeBook
: Remove a book from the inventory by IDsearchBooks
: Search for books by title, author, or genrecalculateTotalValue
: Calculate the total value of all books in inventorygetBooksByGenre
: Get all books of a specific genredisplayBook
: Display a single book's information
4. Creating the Main Application
Create a file called index.ts
in the src
folder that:
- Imports all the types and functions from the other files
- Creates an initial inventory with at least 5 books
- Implements a simple command-line menu system using console.log
- Allows the user to interact with the inventory through the functions created earlier
5. Building and Running the Application
- Add compile and run scripts to the
package.json
file - Compile the TypeScript code to JavaScript
- Run the application and test all the functionality
6. Optional Extensions (for advanced students)
- Add error handling with custom error types
- Implement book editing functionality
- Add persistence by saving the inventory to a JSON file
- Create a more interactive command-line interface using a library like readline
Evaluation Criteria
- Correct use of TypeScript types and interfaces
- Proper implementation of the required functionality
- Clean, well-organized code
- Effective use of TypeScript's type checking
Conclusion
Setting up a proper TypeScript environment is an investment that pays dividends throughout the development process. A well-configured project with appropriate tooling will catch errors early, improve code quality, and enhance the development experience for all team members. As you become more comfortable with TypeScript, you can further customize your setup to match your specific needs and preferences. The configuration described in this guide provides a solid foundation from which you can build increasingly sophisticated applications with confidence. In the next section, we'll explore the key differences between TypeScript and JavaScript to deepen your understanding of how TypeScript enhances the JavaScript language.