Skip to content

An example of how to get started with TypeScript by converting and existing JavaScript project.

Notifications You must be signed in to change notification settings

robertbullen/javascript-to-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

javascript-to-typescript

An example of how to get started with TypeScript by converting an existing JavaScript project.

Functionality

This project implements a simple Node.js + Express server to perform basic arithmetic operations on two or more numbers.

Project Structure

This repository contains two branches:

  • master—The base implementation using JavaScript.
  • typescript—The finished port of the project using TypeScript. Each commit in this branch represents a single conversion step, with the commit message acting as the explanation for what happened and why.

In both branches, there are three code files present:

File Description
index.(js|ts) This file is entry point of the server; it sets up an Express server and registers a request handler for '/calculate'.
payload.(js|ts) Code in this file is responsible for validating that the JSON payload sent by the client is of the expected format.
arithmetic.(js|ts) This file supplies the arithmetic operations.

Both the payload validation and arithmetic operations return Promise-like objects to simulate the asynchronous nature typical of Node.js programming. Payload validation utilizes the Joi library.

Starting the Server

After cloning this repository, simply run these commands to fire up the server:

npm install
npm start

These commands will work regardless of whether you're working in the master or typescript branch. In the typescript branch, ts-node is executed instead of node.

Invoking from a Client

Clients invoke arithmetic operations on the server by sending a POST message to the /calculate endpoint with a payload formatted something like this, which would result in the addition of 1 and 2:

{
    "operation": "+",
    "operands": [1, 2]
}

One way to accomplish this is to use curl at the command line as follows:

curl 'http://localhost:8080/calculate' --silent --header 'Content-Type: application/json' --data '{"operation":"+","operands":[1,2]}' | json_pp

Piping to json_pp (JSON pretty print) is optional but yields a more readable console, especially because it adds a trailing newline so that curl's output isn't buried at the beginning of the next command prompt.

About

An example of how to get started with TypeScript by converting and existing JavaScript project.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published