TypeScript is JavaScript with syntax for types.
TypeScript is a strongly typed programming language which builds on JavaScript giving you better tooling at any scale.
-
TypeScript must be known globally on your machine:
npm install -g typescript
-
Create a new folder and initiate TypeScript configuration:
tsc --init
- Review the fille name
tsconfig.json
that has been generated
- Review the fille name
-
Create a file named
greeter.ts
class Student { fullName: string; constructor(public firstName: string, public middleInitial: string, public lastName: string) { this.fullName = firstName + " " + middleInitial + " " + lastName; } } interface Person { firstName: string; lastName: string; } function greeter(person : Person) { return "Hello, " + person.firstName + " " + person.lastName; } let user = new Student("Jane", "M.", "User"); document.body.innerHTML = greeter(user);
-
Run the compiler:
tsc
- Review the generated file
greeter.js
- You can also have the compiler run with the watch option:
tsc --watch
- Review the generated file
-
Create a
greeter.html
file and open the file in a browser:<!DOCTYPE html> <html> <head><title>TypeScript Greeter</title></head> <body> <script src="greeter.js"></script> </body> </html>