Fastify + Typescript

Edison Devadoss
YavarTechWorks
Published in
4 min readSep 19, 2020

Hi friends, In this article, I will explain the implementation of node application with Fastify and Typescript.

https://www.photopea.com/

What is Typescript

Typescript is the Superset of the JavaScript. Typescript checks the program for errors before execution and does so based on the kinds of values, its a static Type checker. TypeScript is a strongly typed, object-oriented, compiled language. It was designed by Anders Hejlsberg (designer of C#) at Microsoft. TypeScript is both a language and a set of tools. Typescript compiles .ts files into .js files.

Why we need Typescript?

Earlier JavaScript is the language introduced for client-side only. After Node.js comes to JavaScript widely used in server-side also. However, JavaScript code grows it tends to get messier, making it difficult to maintain and reuse the code. Moreover, its failure to embrace the features of Object Orientation, strong type checking and compile-time error checks prevent JavaScript from succeeding at the enterprise level as a full-fledged server-side technology. The typeScript was presented to bridge this gap.

The primary benefits of using TypeScript are to catch more errors before they go into production and make it easier to work with your codebase.

Create a Project with Fastify with Typescript

$ mkdir fastify-typescript
$ cd fastify-typescript
$ npm init -y
$ npm install fastify ts-node --save
$ npm install typescript @types/node nodemon --save-D

Once you install all related dependencies then move to the code.

Create app/application.ts file

$ mkdir app
$ cd app
$ touch application.ts
https://carbon.now.sh/

Create app/index.ts file

$ touch app/index.ts
https://carbon.now.sh/

Configure package.json file for running your application.

If you want to run your Typescript node application you need to build the project. Using the run build method Typescript files are compile to Javascript files.

Create tsconfig.json

https://carbon.now.sh/

In this tsconfig.json file, we defined how it is going to compile Typescript files. Here all of the Typescript files compile and store under the dist directory. And its ignore node_modules directory. For more details to tsconfig follow this link.

Create a nodemon.json

Create a nodemon.json in the root directory. The purpose of the nodemon is if we make any changes in our source code the nodemon compiles the files automatically.

https://carbon.now.sh/

Configure a tslint.json for Typescript error checking before it is going to build.

Install below dev dependencies for Tslint

$ npm install prettier tslint tslint-config-airbnb tslint-config-prettier --save-D

Create a tslint.json file in the root directory.

We can change as per our need. Follow this link for more detail about tslint.

Configure the script in the package.json file

https://carbon.now.sh/

Once we finished all the setup. Then we can run our application.

$ npm run start:watch

If you make any changes in your code it reflects automatically not necessary to build our application again.

--

--

Edison Devadoss
YavarTechWorks

Software developer / JavaScript / React / React Native / Firebase / Node.js / C Programming / Book Reader