TypeScript Installation

npm install -g typescript@latest

tsc -v

Also install typescript locally so that the users of our package will have available the correct version of typescript, for this type

// to create package.json file with all the default configurations
npm  init -y
// now to install typescript locally
npm install --save-dev typescript@latest

// using package.json to check the version of typescript, change the test property to following

  "scripts": {
    "test": "tsc -v"
  }

// type in shell
npm test

// file name is server.ts

const message: string = "hello world";
console.log(message);

// for comilation type
tsc server.ts
// now run the compiled server.js file with
node server.js

add tsconfig.json

Install the express server

npm install --save-dev express

even after installing express it will give us red underline errors.

we have to tell typescript what express javascript library express means in typescript by installing the following package.

npm install --save-dev @types/express

  • type the following to the server/server.ts file to create server
import * as express from "express";

const port: string | number = process.env.port || 1339;

const app = express();

app.listen(port);
app.use(express.static("public"));

console.log("Server is listening on port " + port);
  • create file public/index.html write anything you want to display in index.html file
Previous