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;
year: number;
Void function log(msg: string): void {
}
console.log(msg);
class FlyingCar extends Vehicle implements
}
ISelfDrive {
Const const lives: number = 99;
hasGps: boolean;
drive(latitude: number, longitude: number,
Classes
elevation: number) {
class OrderLogic {
}
constructor(public order: IOrder) { }
getLocation(): number {
getOrderTotal(): number {
}
let sum: number = 0;
}
for (let orderDetail of this.o‐
rder.orderDetails)
Usage
{
Installing TypeScript npm npm install -g typescript
sum += orderDetail.price;
} 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;
Static static log(msg: string) { console.log(msg) };
abstract calcSalary(): number;
}
By Greg Finzer (GregFinzer) Published 9th February, 2017. Sponsored by Readable.com
cheatography.com/gregfinzer/ Last updated 9th February, 2017. Measure your website readability!
www.kellermansoftware.com Page 1 of 2. https://readable.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 {
class Util { logger.log("calculating
log(msg: string, logDate?: Date) { order");
if (logDate) return 0;
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 Readable.com
cheatography.com/gregfinzer/ Last updated 9th February, 2017. Measure your website readability!
www.kellermansoftware.com Page 2 of 2. https://readable.com