How To Set Up a New TypeScript Project

Setting up a new TypeScript project involves several steps including installing TypeScript, initializing the project, setting up configuration files, and writing some initial code. Here’s a detailed guide to help you set up a new TypeScript project:

  1. Install Node.js and npm

    Before starting, ensure that you have Node.js and npm (Node Package Manager) installed. You can download and install them from Node.js official website.

    Verify the installation by running:

                    
                        node -v
                        npm -v                    
                    
                

  2. Create a New Project Directory

    Create a new directory for your TypeScript project and navigate into it:

                    
                        mkdir my-typescript-project
                        cd my-typescript-project                    
                    
                

  3. Initialize the Project

    Initialize the project with npm, which will create a package.json file to manage your project dependencies:

                    
                        npm init -y
                    
                

  4. Install TypeScript

    Install TypeScript as a development dependency:

                    
                        npm install typescript --save-dev
                    
                

  5. Create tsconfig.json

    Initialize a TypeScript configuration file:

                    
                        npx tsc --init
                    
                

    This command creates a tsconfig.json file with default settings. You can customize this file according to your project's requirements. A basic tsconfig.json might look like this:

                    
                        {
                            "compilerOptions": {
                              "target": "es6",
                              "module": "commonjs",
                              "strict": true,
                              "esModuleInterop": true,
                              "skipLibCheck": true,
                              "forceConsistentCasingInFileNames": true,
                              "outDir": "./dist"
                            },
                            "include": ["src/**/*"],
                            "exclude": ["node_modules"]
                          }                      
                    
                

  6. Create Source Directory

    Create a directory for your TypeScript source files:

                    
                        mkdir src
                    
                

  7. Write Initial TypeScript Code

    Create a simple TypeScript file inside the src directory:

                    
                        echo 'const greet = (name: string): string => `Hello, ${name}!`; console.log(greet("World"));' > src/index.ts
                    
                

  8. Compile TypeScript Code

    Compile the TypeScript code to JavaScript:

                    
                        npx tsc
                    
                

    This command will compile the TypeScript files and output the JavaScript files in the dist directory as specified in the tsconfig.json file

  9. Run the Compiled JavaScript Code

    Run the compiled JavaScript code using Node.js:

                    
                        node dist/index.js
                    
                

  10. Automate the Build Process

    You can automate the build process using npm scripts. Edit the package.json to add a build: script

                    
                        "scripts": {
                            "build": "tsc",
                            "start": "node dist/index.js"
                          }
                    
                

    Now, you can build and run your project using:

                    
                        npm run build
                        npm start                    
                    
                

  11. Optional: Set Up a Development Server

    For a better development experience, you can set up a development server using ts-node and nodemon.

    Install ts-node and nodemon

                    
                        npm install ts-node nodemon --save-dev
                    
                

    Add Development Script

    Modify the package.json to include a dev script:

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

    Now, you can start the development server using:

                    
                        npm run dev
                    
                

    This will automatically restart the server whenever you make changes to your TypeScript files.

  12. Additional Configuration

    You can add other configurations and dependencies based on your project needs, such as linting with ESLint, formatting with Prettier, and testing frameworks like Jest.

    Setting Up ESLint

    Install ESLint:

                    
                        npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
                    
                

    Initialize ESLint configuration:

                    
                        npx eslint --init
                    
                

    Choose the options that best fit your project. Here’s an example of an ESLint configuration (.eslintrc.json) for a TypeScript project:

                    
                        {
                            "parser": "@typescript-eslint/parser",
                            "extends": [
                              "eslint:recommended",
                              "plugin:@typescript-eslint/recommended"
                            ],
                            "parserOptions": {
                              "ecmaVersion": 2020,
                              "sourceType": "module"
                            },
                            "rules": {
                              // Customize your linting rules here
                            }
                          }                      
                    
                

By following these steps, you'll have a fully set-up TypeScript project ready for development.

A Guide on Dependency Injection in NestJS

Dependency Injection (DI) is a design pattern that allows classes to define their dependencies without creating them. It's a fundamental concept in building scalable and maintainable applications. NestJS, being a TypeScript framework for building eff …

read more

How To Use Decorators in TypeScript

In TypeScript, decorators provide a way to add both annotations and a meta-programming syntax for classes, methods, properties, or parameters. Decorators are functions that can be used to modify the behavior of these declarations. decorators are a po …

read more