Tutorial

How To Set Up a New TypeScript Project

Updated on May 9, 2022
Default avatar

By Alligator.io

How To Set Up a New TypeScript Project

Introduction

You may have worked with TypeScript before when using a starter project or a tool like the Angular CLI. In this tutorial, you will learn how to set up a TypeScript project without a starter’s help. You will also learn how compiling works in TypeScript and how to use a linter with your TypeScript project.

Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

Prerequisites

To complete this tutorial, you will need the following:

Step 1 — Starting the TypeScript Project

To begin your TypeScript project, you will need to create a directory for your project:

  1. mkdir typescript-project

Now change into your project directory:

  1. cd typescript-project

With your project directory set up, you can install TypeScript:

  1. npm i typescript --save-dev

It is important to include the --save-dev flag because it saves TypeScript as a development dependency. This means that TypeScript is absolutely required for the development of your project.

With TypeScript installed, you can initialize your TypeScript project by using the following command:

  1. npx tsc --init

npm also includes a tool called npx, which will run executable packages. npx allows us to run packages without having to install them globally.

The tsc command is used here because it is the built-in TypeScript compiler. When you write code in TypeScript, running tsc will transform or compile your code into JavaScript.

Using the --init flag in the above command will initialize your project by creating a tsconfig.json file in your typescript-project project directory. This tsconfig.json file will allow you to configure further and customize how TypeScript and the tsc compiler interact. You can remove, add, and change configurations in this file to best meet your needs.

Open tsconfig.json in your editor:

  1. nano tsconfig.json

You, and you will find the default configuration. There will be many options, most of which are commented out:

typescript-project/tsconfig.json
{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Projects */
    // "incremental": true,                              /* Enable incremental compilation */
    // "composite": true,                                /* Enable constraints that allow a TypeScript project to be used with project references. */
    // "tsBuildInfoFile": "./",                          /* Specify the folder for .tsbuildinfo incremental compilation files. */
    // "disableSourceOfProjectReferenceRedirect": true,  /* Disable preferring source files instead of declaration files when referencing composite projects */
    // "disableSolutionSearching": true,                 /* Opt a project out of multi-project reference checking when editing. */
    // "disableReferencedProjectLoad": true,             /* Reduce the number of projects loaded automatically by TypeScript. */
    
    . . .
  }
}

You can customize your TypeScript configuration. In the tsconfig.json file. For instance, you might consider uncommenting the outDir entry and setting it to "./build", which will put all of your compiled TypeScript files into that directory.

With TypeScript installed and your tsconfig.json file in place, you can now move on to coding your TypeScript app and compiling it.

Note: Step 3 below will replace many of your configurations with sensible defaults, but these changes will get you started right away.

Step 2 — Compiling the TypeScript Project

You can now begin coding your TypeScript project. Open a new file named index.ts in youreditor. Write the following TypeScript code in index.ts:

typescript-project/index.ts
const world = 'world';

export function hello(who: string = world): string {
  return `Hello ${who}! `;
}

With this TypeScript code in place, your project is ready to be compiled. Run tsc from your project’s directory:

  1. npx tsc

You will notice that the compiled JavaScript index.js file and the index.js.map sourcemap file have both been added to the build folder if you specified that in the tsconfig.js file.

Open index.js and you will find the following compiled JavaScript code:

typescript-project/build/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hello = void 0;
const world = 'world';
function hello(who = world) {
    return `Hello ${who}! `;
}
exports.hello = hello;

Running the TypeScript compiler every time you make a change can be tedious. To fix this, you can put the compiler in watch mode. Setting the compiler to watch mode means that your TypeScript file will be recompiled every time changes are made.

You can activate watch mode using the following command:

  1. npx tsc -w

You’ve learned how the TypeScript compiler works, and you are now able to successfully compile your TypeScript files. You can take your TypeScript projects to the next level by introducing a linter into your workflow.

Step 3 — Using Google TypeScript Style to Lint and Correct Your Code

Using a linter when coding will help you to quickly find inconsistencies, syntax errors, and omissions in your code. Additionally, a style guide will not only help you ensure that your code is well-formed and consistent but allows you to use additional tools to enforce that style. A common tool for these is eslint, which works well with many IDEs to help during the development process.

With your project now set up, you can use other tools in the TypeScript ecosystem to help and avoid having to set up linting and configuration in the tsconfig.json file by hand. Google TypeScript Style is one such tool. Google TypeScript Style, known as GTS, is a style guide, linter, and automatic code corrector all in one. Using GTS will help you to quickly bootstrap a new TypeScript project and avoid focusing on small, organizational details to focus on designing your project. GTS also offers opinionated default configuration. This means that you won’t have to do much configuration customization.

Begin by installing GTS:

  1. npm i gts --save-dev

From here, initialize GTS using the following command:

  1. npx gts init

The above command will generate everything you need to get started with your TypeScript, including a tsconfig.json file and a linting setup. A package.json file will also be generated if you don’t have one in place already.

Running npx gts init will also add helpful npm scripts to your package.json file. For example, you can now run npm run compile to compile your TypeScript project. To check for linting errors, you can now run npm run check.

Note: Installing TypeScript before installing GTS ensures that you have the most recent version of TypeScript when developing your application. As a result of GTS development moving more slowly than that of TypeScript, you may find that it will suggest downgrading TypeScript in your dev dependencies. You can instruct GTS not to overwrite this value if you need the newer features.

Additionally, as the eslint TypeScript linter has a range of supported versions of TypeScript, newer versions of the language may fall outside of this range. In this case, eslint will warn you of such. There is a good chance that it will continue to work just fine, but if you do run into problems, you can downgrade your version of TypeScript by specifying it when you install. For example, npm i typescript@4.4.2 --save-dev.

GTS is now installed and properly integrated into your TypeScript project. Using GTS on future projects will allow you to quickly set up new TypeScript projects with the necessary configurations in place.

As GTS provides an opinionated, no-configuration approach, it will use its own sensible linting and fixing rules. These follow many best practices, but if you find yourself needing to modify the rules in any way, you can do so by extending the default eslint rules. To do so, create a file in your project directory named .eslintrc which extends the style rules:

---
extends:
  - './node_modules/gts'

This will allow you to add to or modify the style rules provided by GTS.

Conclusion

In this tutorial, you began a TypeScript project with customized configurations. You also integrated Google TypeScript Style into your TypeScript project. Using GTS will help you to quickly get up and running with a new TypeScript project. With GTS, you won’t need to manually set up configuration or integrate a linter into your workflow.

As an additional step, you might be interested in learning how to work with TypeScript in Visual Studio Code. You can also check out this article to learn how to use TypeScript with React.

Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers - for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!

Learn more here


About the authors
Default avatar
Alligator.io

author



Default avatar

Tech Writer at DigitalOcean


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
5 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

At the npx tsc index.ts step, I think you meant to just run npx tsc without the index.ts.

If you do it with the index.ts then it won’t create the dist folder. At least it didn’t for me.

great writing. you are missing a step “npm init” after the directory creation

Hello, we can also use the cli-tool:

$ npx create-typescript-application

Cheers!

when i run npx tsc -w it tells that Parameter ‘world’ cannot reference itself.

Thanks for the introduction to TS. I noticed however that TSlint is deprecated since 2019, maybe rewrite the instructions with steps to get things set up with eslint?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel