TypeScript Cheat Sheet
by Greg Finzer (GregFinzer) via cheatography.com/15280/cs/10814/
Types Inheritance and Implementing Interfaces
String let customerName: string= "John Doe"; interface IGPS {
Number let price: number = 19.95; getLocation() number;
}
Boolean let shipped: boolean = false;
interface ISelfDrive extends IGPS {
Date let orderDate: Date = new Date(2017, 2, 9);
drive(latitude: number, longitude: number,
Any let something: any = "Can be anything";
elevation: number) : void;
Enum enum Color {Red, Green, Blue};
}
Array let cards: string[] = ['Visa', 'MasterCard']; class Vehicle {
Null let orderId: number = null; make: string;
Tuple let stateTaxRates: [string, number]; model: string;
Void function log(msg: string): void { year: number;
console.log(msg); }
} class FlyingCar extends Vehicle implements ISelfDrive
Const const lives: number = 99; {
hasGps: boolean;
Classes drive(latitude: number, longitude: number,
elevation: number) {
class OrderLogic {
constructor(public order: IOrder) { } }
getLocation(): number {
getOrderTotal(): number {
let sum: number = 0; }
}
for (let orderDetail of
this.order.orderDetails)
{ Usage
sum += orderDetail.price; Installing TypeScript npm npm install -g typescript
} Compiling TypeScript tsc somefile.ts
return sum;
TypeScript Docs TypeScriptLang.org
}
Type Definition Files DefinatelyTyped.org
}
Scope/Modifiers
Abstract Classes
Public (default) public firstName: string;
abstract class Person {
Protected protected inventory: number;
name: string;
Private private outOfStock: boolean;
monthlySalary: number;
Read Only readonly pi: number = 3.14159;
monthlyBenefits: number;
abstract calcSalary(): number; Static static log(msg: string) { console.log(msg) };
By Greg Finzer (GregFinzer) Published 9th February, 2017. Sponsored by Readability-Score.com
cheatography.com/gregfinzer/ Last updated 9th February, 2017. Measure your website readability!
www.kellermansoftware.com Page 1 of 2. https://readability-score.com
TypeScript Cheat Sheet
by Greg Finzer (GregFinzer) via cheatography.com/15280/cs/10814/
Interfaces Namespaces
interface IOrderDetail { namespace AcmeCorp.Logging {
productName: string; export class Logger {
quantity: number; static log(msg: string) : void {
price: number; console.log(msg);
orderDate: Date; };
shipped: boolean; }
//Optional }
outOfStock?: boolean; /// <reference path="AcmeCorp.Logging.ts" />
//Method //Alias
calcTax: (taxRate: number) => number; import logger = AcmeCorp.Logging.Logger;
} namespace AcmeCorp.OnlineStore {
class OrderLogic {
Optional Parameters calcOrder(): number {
logger.log("calculating order");
class Util {
return 0;
log(msg: string, logDate?: Date) {
}
if (logDate)
}
console.log(logDate + ' ' + msg);
}
else
console.log(new Date() + ' ' + msg);
}
}
Rest Parameters
class Order {
addOrderDetails(...orderDetails: IOrderDetail[]) {
}
}
By Greg Finzer (GregFinzer) Published 9th February, 2017. Sponsored by Readability-Score.com
cheatography.com/gregfinzer/ Last updated 9th February, 2017. Measure your website readability!
www.kellermansoftware.com Page 2 of 2. https://readability-score.com