Any browser. Any host. Any OS. Open source
like JavaScript but with no surprises
For NPM users:
npm install -g typescript
In your editor just type TypeScript code in
greeter.ts
At the command line, run the TypeScript compiler:
tsc greeter.ts
the result will be a file greeter.js which contains the same JavaScript that you know well
Any type
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
let list: any[] = [1, true, "free"];
list[1] = 100;
Built-in types
let isDone: boolean = false;
let fullName: string = `Bob Bobbington`;
let u: undefined = undefined;
let n: null = null;
function warnUser(): void {
console.log("This is my warning message");
}
function throwError(errorMsg: string): never {
throw new Error(errorMsg);
}
let empId: string | number;
empId = 111; // OK
empId = "E111"; // OK
empId = true; // Compiler Error
enum PrintMedia {
Newspaper = "NEWSPAPER",
Newsletter = "NEWSLETTER",
Magazine = "MAGAZINE",
Book = "BOOK"
}
// Access String Enum
PrintMedia.Newspaper; //returns NEWSPAPER
PrintMedia['Magazine'];//returns MAGAZINE
let list: number[] = [1, 2, 3];
let list: Array <number> = [1, 2, 3];
// Declare a tuple type;
let x: [string, number];
// Initialize it;
x = ["hello", 10]; // OK
// Initialize it incorrectly;
x = [10, "hello"]; // Error
Type 'number' is not assignable to type 'string'.
Type 'string' is not assignable to type 'number'.
function Greet(greeting: string, name: string ) : string {
return greeting + ' ' + name + '!';
}
let sum = (x: number, y: number) => x + y;
Example of TS interface
interface IEmployee {
empCode: number;
empName: string;
empDept?:string;
}
let empObj1:IEmployee = { // OK
empCode:1,
empName:"Steve"
}
let empObj2:IEmployee = { // OK
empCode:1,
empName:"Bill",
empDept:"IT"
}
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
move(distInMeters: number = 0) {
console.log(`${this.name} moved ${distInMeters}m.`);
}
}
class Snake extends Animal {
constructor(name: string) {
super(name);
}
move(distInMeters = 5) {
console.log("Slithering...");
super.move(distInMeters);
}
}
abstract class Person {
abstract name: string;
display(): void{
console.log(this.name);
}
}
class Employee extends Person {
name: string;
empCode: number;
constructor(name: string, code: number) {
super(); // must call super()
this.empCode = code;
this.name = name;
}
}
let emp: Person = new Employee("James", 100);
emp.display(); //James
class Employee {
private empCode: number;
empName: string;
}
let emp = new Employee();
emp.empCode = 123; // Compiler Error
emp.empName = "Swati";//OK
function getArray <T>(items : T[] ) : T[] {
return new Array <T>().concat(items);
}
let myNumArr = getArray <number>([100, 200, 300]);
let myStrArr = getArray <string>(["Hello", "World"]);
myNumArr.push(400); // OK
myStrArr.push("Hello TypeScript"); // OK
myNumArr.push("Hi"); // Compiler Error
myStrArr.push(500); // Compiler Error
TypeScript's getting better all the time. As for 2020 it's among the top ten most wanted programming languages, according to GitHub Google made it the primary language for developing Angular apps. All things considered, TypeScript is a fantastic tool for JavaScript developers. It makes working on larger projects easier and provides a better code-writing toolkit that can greatly improve your software development process. TypeScript is popular, trusted by the biggest players in the industry, and not as difficult as many seem to believe.