Upload
colours-bv
View
129
Download
0
Embed Size (px)
DESCRIPTION
Slides about the presentation of Jan van Helvoort on the DUUG meetup of 25 september 2014 about Typescript.
Citation preview
TYPESCRIPTDUUG 25 SEPTEMBER 2014By Jan van Helvoort - Web Expert / @janvhelvoort
TYPESCRIPT LETS YOU WRITE JAVASCRIPTTHE WAY YOU REALLY WANT TO.
TYPESCRIPTTypesInterfacesClassesModulesFunctionsGenerics
JAVASCRIPTTYPESCRIPT IS A TYPED SUPERSET OF JAVASCRIPT THAT COMPILES TO PLAIN JAVASCRIPT.
BENEFITSCompilerType safetyAuto completionDeclaration files ( )OOPAngular.js Backend
Link
TYPESStringNumberBooleanArrayEnumAnyVoid
STRING / NUMBER / BOOLEANvar name: string = 'Jan';var age: number = 24;var isDutch: boolean = true;
Cannot convert 'boolean' to 'string'.
name = isDutch;
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');
ENUMenum Countries { 'Netherlands', 'Belgium', 'Unkown' }var country: Countries = Countries.Netherlands;
ANYvar notSure: any = 4;notSure = "maybe a string instead";notSure = false; // okay, definitely a boolean
INTERFACESinterface IPerson { Name: string; Country: Countries; IsDutch: boolean;
callName(): string;}
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'.
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; }}
CLASSES - A SHORTER WAYclass person implements IPerson { constructor(public Name: string, public Country: Countries, public IsDutch: boolean) { }
callName() { return this.Name; }}
NEW CLASSvar dave = new person('Dave Woestenborghs', Countries.Belgium, false);
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);
TOOLSVisual Studio 2013 update 2Visual Studio 2012 ( )Sublime Text ( )Emacs ( )Vim ( )
Link Link
Link Link
DEMO