[go: up one dir, main page]

0% found this document useful (0 votes)
8 views2 pages

Gregfinzer Typescript

This TypeScript Cheat Sheet provides a comprehensive overview of TypeScript syntax, including data types, inheritance, interfaces, classes, and usage examples. It covers key concepts such as optional parameters, rest parameters, and namespaces, along with practical code snippets. The document is authored by Greg Finzer and was last updated on February 9, 2017.

Uploaded by

Josue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Gregfinzer Typescript

This TypeScript Cheat Sheet provides a comprehensive overview of TypeScript syntax, including data types, inheritance, interfaces, classes, and usage examples. It covers key concepts such as optional parameters, rest parameters, and namespaces, along with practical code snippets. The document is authored by Greg Finzer and was last updated on February 9, 2017.

Uploaded by

Josue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

TypeScript Cheat Sheet

by Greg Finzer (GregFinzer) via cheatography.com/15280/cs/10814/

Types Inheri​tance and Implem​enting Interfaces

String let custom​erName: string= "John Doe"; interface IGPS {


Number let price: number = 19.95; ​ ​get​Loc​ation() number;
}
Boolean let shipped: boolean = false;
interface ISelfDrive extends IGPS {
Date let orderDate: Date = new Date(2017, 2, 9);
​ ​dri​ve(​lat​itude: number, longitude: number,
Any let something: any = "Can be anythi​ng";
elevation: number) : void;
Enum enum Color {Red, Green, Blue}; }
Array let cards: string[] = ['Visa', 'Maste​rCa​rd']; class Vehicle {
Null let orderId: number = null; ​ ​make: string;

Tuple let stateT​axR​ates: [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;
​ ​dri​ve(​lat​itude: number, longitude: number,
Classes
elevation: number) {
class OrderLogic {
​ }
​ ​ ​ ​con​str​uct​or(​public order: IOrder) { }
​ ​get​Loc​ati​on(): number {
​ ​ ​ ​get​Ord​erT​otal(): number {
​ }
​ ​ ​ ​ ​ ​ ​ let sum: number = 0;
}
​ ​ ​ ​ ​ ​ ​ for (let orderD​etail of this.o​‐
rde​r.o​rde​rDe​tails)
Usage
​ ​ ​ ​ ​ ​ ​ {
Installing TypeScript npm npm install -g typescript
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ sum += orderD​eta​il.p​rice;
​ ​ ​ ​ ​ ​ ​ } Compiling TypeScript tsc somefi​le.ts

​ ​ ​ ​ ​ ​ ​ ​return sum; TypeScript Docs TypeSc​rip​tLa​ng.org


​ ​ ​ } Type Definition Files Defina​tel​yTy​ped.org
}
Scope/​Mod​ifiers
Abstract Classes Public (default) public firstName: string;
abstract class Person { Protected protected inventory: number;
​ ​name: string;
Private private outOfS​tock: boolean;
​ ​mon​thl​ySa​lary: number;
Read Only readonly pi: number = 3.14159;
​ ​mon​thl​yBe​nefits: number;
Static static log(msg: string) { consol​e.l​og(msg) };
​ ​abs​tract calcSa​lary(): 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 {


​ ​pro​duc​tName: string; ​ ​export class Logger {
​ ​qua​ntity: number; ​ ​ ​ ​ ​ ​ ​static log(msg: string) : void {
​ ​price: number; ​ ​ ​ ​ ​ ​ ​ ​con​sol​e.l​og(​msg);
​ ​ord​erDate: Date; ​ ​ ​ ​ ​ };
​ ​shi​pped: boolean; ​ }
​ ​//O​ptional }
​ ​out​OfS​tock?: boolean; /// <re​ference path="A​cme​Cor​p.L​ogg​ing.ts​" />
​ ​//M​ethod //Alias
​ ​cal​cTax: (taxRate: number) => number; import logger = AcmeCo​rp.L​og​gin​g.L​ogger;
} namespace AcmeCo​rp.O​nl​ine​Store {
​ ​class OrderLogic {
Optional Parameters ​ ​ ​ ​cal​cOr​der(): number {

class Util { ​ ​ ​ ​ ​ ​ ​ ​log​ger.lo​g("c​alc​ulating

​ ​log​(msg: string, logDate?: Date) { order");

​ ​ ​ if (logDate) ​ ​ ​ ​ ​ ​ ​ ​return 0;

​ ​ ​ ​ ​ ​con​sol​e.l​og(​logDate + ' ' + msg); ​ ​ ​ }

​ ​ ​ else ​ }

​ ​ ​ ​ ​ ​con​sol​e.l​og(new Date() + ' ' + msg); }

​ }
}

Rest Parameters

class Order {
​ ​add​Ord​erD​eta​ils​(...or​der​Det​ails:
IOrder​Det​ail[]) {
​ }
}

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

You might also like