18
TYPESCRIPT DUUG 25 SEPTEMBER 2014

DUUG meetup Colours - Typescript

Embed Size (px)

DESCRIPTION

Slides about the presentation of Jan van Helvoort on the DUUG meetup of 25 september 2014 about Typescript.

Citation preview

Page 1: DUUG meetup Colours - Typescript

TYPESCRIPTDUUG 25 SEPTEMBER 2014By Jan van Helvoort - Web Expert / @janvhelvoort

Page 2: DUUG meetup Colours - Typescript

TYPESCRIPT LETS YOU WRITE JAVASCRIPTTHE WAY YOU REALLY WANT TO.

Page 3: DUUG meetup Colours - Typescript

TYPESCRIPTTypesInterfacesClassesModulesFunctionsGenerics

Page 4: DUUG meetup Colours - Typescript

JAVASCRIPTTYPESCRIPT IS A TYPED SUPERSET OF JAVASCRIPT THAT COMPILES TO PLAIN JAVASCRIPT.

Page 5: DUUG meetup Colours - Typescript

BENEFITSCompilerType safetyAuto completionDeclaration files ( )OOPAngular.js Backend

Link

Page 6: DUUG meetup Colours - Typescript

TYPESStringNumberBooleanArrayEnumAnyVoid

Page 7: DUUG meetup Colours - Typescript

STRING / NUMBER / BOOLEANvar name: string = 'Jan';var age: number = 24;var isDutch: boolean = true;

Cannot convert 'boolean' to 'string'.

name = isDutch;

Page 8: DUUG meetup Colours - Typescript

ARRAYvar list: number[] = [1, 2, 3];list.push(4);

Could not apply type 'number' to argument 1 which is of type 'string'.

list.push('five');

Page 9: DUUG meetup Colours - Typescript

ENUMenum Countries { 'Netherlands', 'Belgium', 'Unkown' }var country: Countries = Countries.Netherlands;

Page 10: DUUG meetup Colours - Typescript

ANYvar notSure: any = 4;notSure = "maybe a string instead";notSure = false; // okay, definitely a boolean

Page 11: DUUG meetup Colours - Typescript

INTERFACESinterface IPerson { Name: string; Country: Countries; IsDutch: boolean;

callName(): string;}

Page 12: DUUG meetup Colours - Typescript

INTERFACESclass person implements IPerson { }

Class person declares interface IPerson but does not implement it:Type 'person' is missing property 'Name' from type 'IPerson'.Type 'person' is missing property 'Counties' from type 'IPerson'.Type 'person' is missing property 'IsDutch' from type 'IPerson'.Type 'person' is missing property 'callName' from type 'IPerson'.

Page 13: DUUG meetup Colours - Typescript

CLASSESclass person implements IPerson { public Name: string; public Country: Countries; public IsDutch: boolean;

constructor(name: string, country: Countries, isDutch: boolean) { this.Name = name; this.Country = Countries.Netherlands; this.IsDutch = isDutch; }

callName() { return this.Name; }}

Page 14: DUUG meetup Colours - Typescript

CLASSES - A SHORTER WAYclass person implements IPerson { constructor(public Name: string, public Country: Countries, public IsDutch: boolean) { }

callName() { return this.Name; }}

Page 15: DUUG meetup Colours - Typescript

NEW CLASSvar dave = new person('Dave Woestenborghs', Countries.Belgium, false);

Page 16: DUUG meetup Colours - Typescript

GENERICSfunction identity<T>(arg: T): T { return arg;}

var output = this.identity<string>('one');

Supplied parameters do not match any signature of call target:Could not apply type 'string' to argument 1 which is of type 'number'.

var output = this.identity<string>(1);

Page 18: DUUG meetup Colours - Typescript

DEMO