SlideShare una empresa de Scribd logo
1 de 36
Working with TypeScript
Oswald Campesato
Consultant/Training:
www.iquarkt.com
ocampesato@yahoo.com
What is TypeScript?
• Microsoft created TypeScript in October/2012
• Supports ES6
• Supports interfaces and sub-interfaces
• Supports classes and subclasses
• Optional type system
• Angular 2 is written in Typescript
https://scotch.io/tutorials/why-you-shouldnt-be-scared-of-
typescript8
More TypeScript Details
• A compiled language (tsc compiler)
• strong typing and also type inferencing
• type checking during compile time
• "minimal" extra compile time overhead
• tsc transpiles ".ts" files into ".js" files
• [sudo] npm install –g typescript // <- installs “tsc”
Links with TypeScript Details
• Official TypeScript website:
http://www.typescriptlang.org/
• Typings: TypeScript Definition Manager:
https://github.com/typings/typings
Definitely Typed: 3rd-party TypeScript Definitions:
http://definitelytyped.org/
Browser Support for TypeScript
• Browsers support only ECMA5
• TypeScript is transpiled to ECMA5
• ES6 is transpiled to ECMA5
• TypeScript playground:
https://www.typescriptlang.org/play/
• tsc is the TypeScript compiler (can also use Babel)
TypeScript Exposes 9 Types
• Boolean
• Number
• String
• Array
• Tuple
• Enum
• Any
• Void
• Function
Additions to TypeScript
• Types
• Classes
• Annotations
• Imports
• language utilities (e.g. destructuring)
• interfaces
• inheritance
• generics
Simple Types in TypeScript
• var isDone: boolean = false;
• var height: number = 6;
• var name: string = "dave";
• var myList:number[] = [1, 2, 3]; // option #1
• var myList:Array<number> = [1, 2, 3]; // option #2
• var changeMe: any = 4;
• changeMe = ”I’m a string now";
• var myList:any[] = [1, true, ”pizza"];
ECMA5 vs TypeScript Functions
• ECMA5 version:
• function add(a,b) { return a+b; }
• add(1,4); // 5
• add(1,’4'); // 14 (valid in ECMA5)
• TypeScript version:
• function add(a:number, b:number) { return a+b; }
• add(1,4); // 5
• add(1,’4'); // (error in TypeScript)
Functions in TypeScript
• msg is an argument of type string and return type is
void in the myLogger function:
• function myLogger(msg?:string): void {
• console.log("My custom logger: “+msg);
• }
• Note: the trailing question mark (“?”) after msg
indicates that msg is an optional argument
Simple Iteration: Factorial
• factorial(n) {
• var prod = 1;
• for(var i=1; i<=n; n++) {
• prod *= i;
• console.log(“factorial “+i+” = “+prod);
• }
• return prod;
• }
• console.log(“factorial 5 = “+factorial(5));
• console.log(“factorial 10 = “+factorial(10));
Exercises: Iteration
• 1) EvenP: calculate the product of the even integers
between 1 and n
• 2) Prime: check if the positive integer n is prime (its
only divisors are 1 and itself)
• 3) print the primes between 2 and 100
• 4) Goldbach’s conjecture: every even number greater
than 2 can be written as a sum of two odd primes:
write a function to show it’s true up to 100 (use #3)
• 5) use #4 to find even numbers with multiple
solutions, such as 14 (=3+11 = 7+7)
Simple Recursion: Factorial
• factorial(n) {
• if(n <= 1) return 1;
• else return n * factorial(n-1)
• }
• console.log(“factorial 5 = “+factorial(5));
• console.log(“factorial 10 = “+factorial(10));
Tail Recursion: Factorial
• factorial(n, prod) {
• if(n <= 1) return prod;
• else return factorial(n-1, n*prod)
• }
• console.log(“factorial 5 = “+factorial(5,1));
• console.log(“factorial 10 = “+factorial(10,1));
Exercises: Recursion
1) use Euclid’s algorithm to find the GCD of two positive
integers (GCD = Greatest Common Divisor)
Example: GCD(10,24) = 2
2) use #1 to find the LCM of two positive integers
(where LCM = Lowest Common Multiple)
Note: LCM(a,b) = a*b/gcd(a,b)
Example: LCM(10,24) = 120
3) Use recursion to calculate Fibonacci(20)
Generics in TypeScript
• Similar to a generic in other languages:
• function identity<T>(arg: T): T {
• return arg;
• }
•
• // output has 'string' type (explicit/inferred):
• var output = identity<string>("Dave");
• var output = identity("Dave");
tsc: Transpiles TypeScript to ECMA5
• 1) version number:
tsc –version
• 2) compile the TS file app.ts:
tsc app.ts (will generate app.js)
• 3) watch for files changes in app.ts:
a) tsc –-watch app.ts
b) tsc –-watch –out bundle.js app.ts
4) tsc –help lists the available options
Output from tsc --init
• {
• "compilerOptions": {
• "module": "commonjs",
• "target": "es3",
• "noImplicitAny": false,
• "outDir": "built",
• "rootDir": ".",
• "sourceMap": false
• },
• "exclude": [
• "node_modules"
• ]
• } // ‘tsc‘ will now compile all TS files in directory
Sample tsconfig.json for Angular 2
• {
• "version": "1.7.5",
• "compilerOptions": {
• "emitDecoratorMetadata": true,
• "experimentalDecorators": true,
• "target": "es5",
• "module": "system",
• "moduleResolution": "node",
• "removeComments": true,
• "sourceMap": true,
• "outDir": "dist"
• },
• "exclude": [
• "node_modules"
• ]
• }
Compilation with tsc
• 1) create the following class in app.ts:
class MyClass { }
• 2) compile the previous class:
tsc app.ts
• 3) step #2 generates this code in app.js:
var MyClass = (function () {
function MyClass() {
}
return MyClass;
})();
Interfaces in TypeScript (1)
interface IUser {
fname: string;
lname: string;
weight?: number; // optional
}
function displayUser(user: IUser) {
console.log("First name = "+user.fname);
}
Interfaces in TypeScript (2)
interface IUser {
fname: string;
lname: string;
weight?: number; // optional
}
// the following code works correctly:
var aUser =
{fname: "John", lname: "Smith", weight:200};
displayUser(aUser);
ES6 Class versus TypeScript Class
class ES6Class { // ES6 Class
constructor(items) { // no type definition
this.items = items;
}
}
let mye6s = new ES6Class([1,2,3]);
console.log(myes6s.items);
class TSClass { // TypeScript Class
items: string[];
constructor(items: string[]) { this.items = items; }
}
let myts = new TSClass([1,2,3]);
console.log(myts.items);
Classes in TypeScript (1)
class User {
fname: string;
lname: string;
weight: number;
constructor(fname:string, lname:string, weight:number) {
this.fname = fname;
this.lname = lname;
this.weight = weight;
}
fullname():string {
return (this.fname+" "+this.lname+" weighs "+this.weight);
}
}
Classes in TypeScript (2)
var u1:User, u2:User;
u1 = new User("Jane", "Smith");
u2 = new User("John", "Jones");
console.log("user1 = "+u1.fullname());
console.log("user2 = "+u2.fullname());
JSON to a TypeScript Class (1)
var people = [
{fname:"Jane",lname:"Smith",zip:"94043"},
{fname:"John",lname:"Jones",zip:"94539"},
{fname:"Dave",lname:"Starr",zip:"67800"}
]
JSON to a TypeScript Class (2)
class Person {
//unnecessary because constructor specifies “public”:
//fname:string;
//lname:string;
//zip:string;
constructor(public fname:string,
public lname:string,
public zip:string) {
this.fname = fname;
this.lname = lname;
this.zip = zip;
}
}
JSON to a TypeScript Class (3)
var TSPeople = [
new Person("Jane","Smith","94043"),
new Person("John","Jones","94539"),
new Person("Dave","Starr","67800")
];
Implementing TypeScript Interfaces (1)
Interface IRect = {
width: number;
height: number;
perimeter: number;
area: number;
}
Implementing TypeScript Interfaces (2)
class Rectangle implements IRect {
constructor(public width:number,
public height:number) {
this.width = width;
this.height = height;
}
public getArea() { return width*height }
public getPerimeter() { return 2*(width+height) }
}
Implementing TypeScript Interfaces (3)
var rect1 = new Rectangle(5,10);
console.log(“area = “+rect1.getArea());
console.log(“perimeter = “+rect1.getPerimeter());
var rect2 = new Rectangle(12,20);
console.log(“area = “+rect2.getArea());
console.log(“perimeter = “+rect2.getPerimeter());
TypeScript Subclasses
class Square extends Rectangle {
constructor(public side:number, width:number, height:number) {
super(width, height);
this.side = side;
}
area(): void {}
perimeter(): void {}
}
var square1 = new Square(20);
console.log("area = "+square1.getArea());
console.log("perimeter = "+square1.getPerimeter());
Exercises for Interfaces/Classes
1) Define an interface IPerson that contains a first
name, last name, and zip code
2) Create a Person2 class that implements IPerson:
class Person2 implements IPerson { … }
3) Modify IPerson so that it contains an optional
weight property
IDEs for TypeScript
WebStorm IDE (30-day trial)
Visual Studio Code (free and cross-platform)
TypEcs (TypeScript IDE for Eclipse):
http://typecsdev.com/
http://definitelytyped.org/directory/tools.html
TypeScript Versions
• https://github.com/Microsoft/TypeScript/wiki/Ro
admap
• Navigate to website to see list of features for:
• TS 2.1
• TS 2.0
• TS 1.9
• TS 1.8
• TS 1.7
• TS 1.6
Recent/Upcoming Books and Training
1) HTML5 Canvas and CSS3 Graphics (2013)
2) jQuery, CSS3, and HTML5 for Mobile (2013)
3) HTML5 Pocket Primer (2013)
4) jQuery Pocket Primer (2013)
5) HTML5 Mobile Pocket Primer (2014)
6) D3 Pocket Primer (2015)
7) Python Pocket Primer (2015)
8) SVG Pocket Primer (2016)
9) CSS3 Pocket Primer (2016)
10) Angular 2 Pocket Primer (2016)

Más contenido relacionado

La actualidad más candente

Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2Knoldus Inc.
 
TypeScript Modules
TypeScript ModulesTypeScript Modules
TypeScript ModulesNoam Kfir
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersRutenis Turcinas
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScriptGil Fink
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript FundamentalsSunny Sharma
 
TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript applicationTypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript applicationAndrea Paciolla
 
Typescript: enjoying large scale browser development
Typescript: enjoying large scale browser developmentTypescript: enjoying large scale browser development
Typescript: enjoying large scale browser developmentJoost de Vries
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java DevelopersYakov Fain
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type scriptRemo Jansen
 
Typescript tips & tricks
Typescript tips & tricksTypescript tips & tricks
Typescript tips & tricksOri Calvo
 
Typescript 101 introduction
Typescript 101   introductionTypescript 101   introduction
Typescript 101 introductionBob German
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviWinston Levi
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins Udaya Kumar
 

La actualidad más candente (20)

TypeScript
TypeScriptTypeScript
TypeScript
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
 
TypeScript Modules
TypeScript ModulesTypeScript Modules
TypeScript Modules
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack Developers
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
 
TypeScript 101
TypeScript 101TypeScript 101
TypeScript 101
 
TypeScript 2 in action
TypeScript 2 in actionTypeScript 2 in action
TypeScript 2 in action
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
 
TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript applicationTypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
 
Typescript: enjoying large scale browser development
Typescript: enjoying large scale browser developmentTypescript: enjoying large scale browser development
Typescript: enjoying large scale browser development
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
Introducing TypeScript
Introducing TypeScriptIntroducing TypeScript
Introducing TypeScript
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
 
Typescript tips & tricks
Typescript tips & tricksTypescript tips & tricks
Typescript tips & tricks
 
Typescript 101 introduction
Typescript 101   introductionTypescript 101   introduction
Typescript 101 introduction
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Learning typescript
Learning typescriptLearning typescript
Learning typescript
 

Similar a TypeScript

A Replay Approach to Software Validation
A Replay Approach to Software ValidationA Replay Approach to Software Validation
A Replay Approach to Software ValidationJames Pascoe
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on PythonSumit Raj
 
スマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみた
スマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみたスマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみた
スマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみたTaro Matsuzawa
 
Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security ProfessionalsAditya Shankar
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScriptDenis Voituron
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개Reagan Hwang
 
Monitoring and Debugging your Live Applications
Monitoring and Debugging your Live ApplicationsMonitoring and Debugging your Live Applications
Monitoring and Debugging your Live ApplicationsRobert Coup
 
CoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseCoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseAlexander Galkin
 
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...Cyber Security Alliance
 
シェル芸でライフハック(特論)
シェル芸でライフハック(特論)シェル芸でライフハック(特論)
シェル芸でライフハック(特論)Yuki Shimazaki
 
Webinar alain-2009-03-04-clamav
Webinar alain-2009-03-04-clamavWebinar alain-2009-03-04-clamav
Webinar alain-2009-03-04-clamavthc2cat
 
The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181Mahmoud Samir Fayed
 

Similar a TypeScript (20)

Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
A Replay Approach to Software Validation
A Replay Approach to Software ValidationA Replay Approach to Software Validation
A Replay Approach to Software Validation
 
Angular2
Angular2Angular2
Angular2
 
Golang
GolangGolang
Golang
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on Python
 
スマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみた
スマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみたスマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみた
スマートフォン勉強会@関東 #11 どう考えてもdisconなものをiPhoneに移植してみた
 
Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security Professionals
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScript
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개
 
Monitoring and Debugging your Live Applications
Monitoring and Debugging your Live ApplicationsMonitoring and Debugging your Live Applications
Monitoring and Debugging your Live Applications
 
CSV JSON and XML files in Python.pptx
CSV JSON and XML files in Python.pptxCSV JSON and XML files in Python.pptx
CSV JSON and XML files in Python.pptx
 
CoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseCoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming course
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
Modern C++
Modern C++Modern C++
Modern C++
 
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
 
シェル芸でライフハック(特論)
シェル芸でライフハック(特論)シェル芸でライフハック(特論)
シェル芸でライフハック(特論)
 
Webinar alain-2009-03-04-clamav
Webinar alain-2009-03-04-clamavWebinar alain-2009-03-04-clamav
Webinar alain-2009-03-04-clamav
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181
 

Más de Oswald Campesato

Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)Oswald Campesato
 
Introduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasIntroduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasOswald Campesato
 
Introduction to Deep Learning
Introduction to Deep LearningIntroduction to Deep Learning
Introduction to Deep LearningOswald Campesato
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2Oswald Campesato
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2Oswald Campesato
 
"An Introduction to AI and Deep Learning"
"An Introduction to AI and Deep Learning""An Introduction to AI and Deep Learning"
"An Introduction to AI and Deep Learning"Oswald Campesato
 
Introduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowIntroduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowOswald Campesato
 
Introduction to Deep Learning for Non-Programmers
Introduction to Deep Learning for Non-ProgrammersIntroduction to Deep Learning for Non-Programmers
Introduction to Deep Learning for Non-ProgrammersOswald Campesato
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your BrowserOswald Campesato
 
Deep Learning in Your Browser
Deep Learning in Your BrowserDeep Learning in Your Browser
Deep Learning in Your BrowserOswald Campesato
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlowOswald Campesato
 
Introduction to Deep Learning and TensorFlow
Introduction to Deep Learning and TensorFlowIntroduction to Deep Learning and TensorFlow
Introduction to Deep Learning and TensorFlowOswald Campesato
 
Intro to Deep Learning, TensorFlow, and tensorflow.js
Intro to Deep Learning, TensorFlow, and tensorflow.jsIntro to Deep Learning, TensorFlow, and tensorflow.js
Intro to Deep Learning, TensorFlow, and tensorflow.jsOswald Campesato
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlowOswald Campesato
 
Introduction to Deep Learning and Tensorflow
Introduction to Deep Learning and TensorflowIntroduction to Deep Learning and Tensorflow
Introduction to Deep Learning and TensorflowOswald Campesato
 
Deep Learning, Scala, and Spark
Deep Learning, Scala, and SparkDeep Learning, Scala, and Spark
Deep Learning, Scala, and SparkOswald Campesato
 
Deep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLDeep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLOswald Campesato
 
Deep Learning, Keras, and TensorFlow
Deep Learning, Keras, and TensorFlowDeep Learning, Keras, and TensorFlow
Deep Learning, Keras, and TensorFlowOswald Campesato
 

Más de Oswald Campesato (20)

Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)
 
Introduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasIntroduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and Keras
 
Introduction to Deep Learning
Introduction to Deep LearningIntroduction to Deep Learning
Introduction to Deep Learning
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2
 
"An Introduction to AI and Deep Learning"
"An Introduction to AI and Deep Learning""An Introduction to AI and Deep Learning"
"An Introduction to AI and Deep Learning"
 
H2 o berkeleydltf
H2 o berkeleydltfH2 o berkeleydltf
H2 o berkeleydltf
 
Introduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowIntroduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and Tensorflow
 
Introduction to Deep Learning for Non-Programmers
Introduction to Deep Learning for Non-ProgrammersIntroduction to Deep Learning for Non-Programmers
Introduction to Deep Learning for Non-Programmers
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your Browser
 
Deep Learning in Your Browser
Deep Learning in Your BrowserDeep Learning in Your Browser
Deep Learning in Your Browser
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlow
 
Introduction to Deep Learning and TensorFlow
Introduction to Deep Learning and TensorFlowIntroduction to Deep Learning and TensorFlow
Introduction to Deep Learning and TensorFlow
 
Intro to Deep Learning, TensorFlow, and tensorflow.js
Intro to Deep Learning, TensorFlow, and tensorflow.jsIntro to Deep Learning, TensorFlow, and tensorflow.js
Intro to Deep Learning, TensorFlow, and tensorflow.js
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlow
 
Introduction to Deep Learning and Tensorflow
Introduction to Deep Learning and TensorflowIntroduction to Deep Learning and Tensorflow
Introduction to Deep Learning and Tensorflow
 
Deep Learning, Scala, and Spark
Deep Learning, Scala, and SparkDeep Learning, Scala, and Spark
Deep Learning, Scala, and Spark
 
Deep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLDeep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGL
 
Deep Learning, Keras, and TensorFlow
Deep Learning, Keras, and TensorFlowDeep Learning, Keras, and TensorFlow
Deep Learning, Keras, and TensorFlow
 
C++ and Deep Learning
C++ and Deep LearningC++ and Deep Learning
C++ and Deep Learning
 

Último

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 

Último (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 

TypeScript

  • 1. Working with TypeScript Oswald Campesato Consultant/Training: www.iquarkt.com ocampesato@yahoo.com
  • 2. What is TypeScript? • Microsoft created TypeScript in October/2012 • Supports ES6 • Supports interfaces and sub-interfaces • Supports classes and subclasses • Optional type system • Angular 2 is written in Typescript https://scotch.io/tutorials/why-you-shouldnt-be-scared-of- typescript8
  • 3. More TypeScript Details • A compiled language (tsc compiler) • strong typing and also type inferencing • type checking during compile time • "minimal" extra compile time overhead • tsc transpiles ".ts" files into ".js" files • [sudo] npm install –g typescript // <- installs “tsc”
  • 4. Links with TypeScript Details • Official TypeScript website: http://www.typescriptlang.org/ • Typings: TypeScript Definition Manager: https://github.com/typings/typings Definitely Typed: 3rd-party TypeScript Definitions: http://definitelytyped.org/
  • 5. Browser Support for TypeScript • Browsers support only ECMA5 • TypeScript is transpiled to ECMA5 • ES6 is transpiled to ECMA5 • TypeScript playground: https://www.typescriptlang.org/play/ • tsc is the TypeScript compiler (can also use Babel)
  • 6. TypeScript Exposes 9 Types • Boolean • Number • String • Array • Tuple • Enum • Any • Void • Function
  • 7. Additions to TypeScript • Types • Classes • Annotations • Imports • language utilities (e.g. destructuring) • interfaces • inheritance • generics
  • 8. Simple Types in TypeScript • var isDone: boolean = false; • var height: number = 6; • var name: string = "dave"; • var myList:number[] = [1, 2, 3]; // option #1 • var myList:Array<number> = [1, 2, 3]; // option #2 • var changeMe: any = 4; • changeMe = ”I’m a string now"; • var myList:any[] = [1, true, ”pizza"];
  • 9. ECMA5 vs TypeScript Functions • ECMA5 version: • function add(a,b) { return a+b; } • add(1,4); // 5 • add(1,’4'); // 14 (valid in ECMA5) • TypeScript version: • function add(a:number, b:number) { return a+b; } • add(1,4); // 5 • add(1,’4'); // (error in TypeScript)
  • 10. Functions in TypeScript • msg is an argument of type string and return type is void in the myLogger function: • function myLogger(msg?:string): void { • console.log("My custom logger: “+msg); • } • Note: the trailing question mark (“?”) after msg indicates that msg is an optional argument
  • 11. Simple Iteration: Factorial • factorial(n) { • var prod = 1; • for(var i=1; i<=n; n++) { • prod *= i; • console.log(“factorial “+i+” = “+prod); • } • return prod; • } • console.log(“factorial 5 = “+factorial(5)); • console.log(“factorial 10 = “+factorial(10));
  • 12. Exercises: Iteration • 1) EvenP: calculate the product of the even integers between 1 and n • 2) Prime: check if the positive integer n is prime (its only divisors are 1 and itself) • 3) print the primes between 2 and 100 • 4) Goldbach’s conjecture: every even number greater than 2 can be written as a sum of two odd primes: write a function to show it’s true up to 100 (use #3) • 5) use #4 to find even numbers with multiple solutions, such as 14 (=3+11 = 7+7)
  • 13. Simple Recursion: Factorial • factorial(n) { • if(n <= 1) return 1; • else return n * factorial(n-1) • } • console.log(“factorial 5 = “+factorial(5)); • console.log(“factorial 10 = “+factorial(10));
  • 14. Tail Recursion: Factorial • factorial(n, prod) { • if(n <= 1) return prod; • else return factorial(n-1, n*prod) • } • console.log(“factorial 5 = “+factorial(5,1)); • console.log(“factorial 10 = “+factorial(10,1));
  • 15. Exercises: Recursion 1) use Euclid’s algorithm to find the GCD of two positive integers (GCD = Greatest Common Divisor) Example: GCD(10,24) = 2 2) use #1 to find the LCM of two positive integers (where LCM = Lowest Common Multiple) Note: LCM(a,b) = a*b/gcd(a,b) Example: LCM(10,24) = 120 3) Use recursion to calculate Fibonacci(20)
  • 16. Generics in TypeScript • Similar to a generic in other languages: • function identity<T>(arg: T): T { • return arg; • } • • // output has 'string' type (explicit/inferred): • var output = identity<string>("Dave"); • var output = identity("Dave");
  • 17. tsc: Transpiles TypeScript to ECMA5 • 1) version number: tsc –version • 2) compile the TS file app.ts: tsc app.ts (will generate app.js) • 3) watch for files changes in app.ts: a) tsc –-watch app.ts b) tsc –-watch –out bundle.js app.ts 4) tsc –help lists the available options
  • 18. Output from tsc --init • { • "compilerOptions": { • "module": "commonjs", • "target": "es3", • "noImplicitAny": false, • "outDir": "built", • "rootDir": ".", • "sourceMap": false • }, • "exclude": [ • "node_modules" • ] • } // ‘tsc‘ will now compile all TS files in directory
  • 19. Sample tsconfig.json for Angular 2 • { • "version": "1.7.5", • "compilerOptions": { • "emitDecoratorMetadata": true, • "experimentalDecorators": true, • "target": "es5", • "module": "system", • "moduleResolution": "node", • "removeComments": true, • "sourceMap": true, • "outDir": "dist" • }, • "exclude": [ • "node_modules" • ] • }
  • 20. Compilation with tsc • 1) create the following class in app.ts: class MyClass { } • 2) compile the previous class: tsc app.ts • 3) step #2 generates this code in app.js: var MyClass = (function () { function MyClass() { } return MyClass; })();
  • 21. Interfaces in TypeScript (1) interface IUser { fname: string; lname: string; weight?: number; // optional } function displayUser(user: IUser) { console.log("First name = "+user.fname); }
  • 22. Interfaces in TypeScript (2) interface IUser { fname: string; lname: string; weight?: number; // optional } // the following code works correctly: var aUser = {fname: "John", lname: "Smith", weight:200}; displayUser(aUser);
  • 23. ES6 Class versus TypeScript Class class ES6Class { // ES6 Class constructor(items) { // no type definition this.items = items; } } let mye6s = new ES6Class([1,2,3]); console.log(myes6s.items); class TSClass { // TypeScript Class items: string[]; constructor(items: string[]) { this.items = items; } } let myts = new TSClass([1,2,3]); console.log(myts.items);
  • 24. Classes in TypeScript (1) class User { fname: string; lname: string; weight: number; constructor(fname:string, lname:string, weight:number) { this.fname = fname; this.lname = lname; this.weight = weight; } fullname():string { return (this.fname+" "+this.lname+" weighs "+this.weight); } }
  • 25. Classes in TypeScript (2) var u1:User, u2:User; u1 = new User("Jane", "Smith"); u2 = new User("John", "Jones"); console.log("user1 = "+u1.fullname()); console.log("user2 = "+u2.fullname());
  • 26. JSON to a TypeScript Class (1) var people = [ {fname:"Jane",lname:"Smith",zip:"94043"}, {fname:"John",lname:"Jones",zip:"94539"}, {fname:"Dave",lname:"Starr",zip:"67800"} ]
  • 27. JSON to a TypeScript Class (2) class Person { //unnecessary because constructor specifies “public”: //fname:string; //lname:string; //zip:string; constructor(public fname:string, public lname:string, public zip:string) { this.fname = fname; this.lname = lname; this.zip = zip; } }
  • 28. JSON to a TypeScript Class (3) var TSPeople = [ new Person("Jane","Smith","94043"), new Person("John","Jones","94539"), new Person("Dave","Starr","67800") ];
  • 29. Implementing TypeScript Interfaces (1) Interface IRect = { width: number; height: number; perimeter: number; area: number; }
  • 30. Implementing TypeScript Interfaces (2) class Rectangle implements IRect { constructor(public width:number, public height:number) { this.width = width; this.height = height; } public getArea() { return width*height } public getPerimeter() { return 2*(width+height) } }
  • 31. Implementing TypeScript Interfaces (3) var rect1 = new Rectangle(5,10); console.log(“area = “+rect1.getArea()); console.log(“perimeter = “+rect1.getPerimeter()); var rect2 = new Rectangle(12,20); console.log(“area = “+rect2.getArea()); console.log(“perimeter = “+rect2.getPerimeter());
  • 32. TypeScript Subclasses class Square extends Rectangle { constructor(public side:number, width:number, height:number) { super(width, height); this.side = side; } area(): void {} perimeter(): void {} } var square1 = new Square(20); console.log("area = "+square1.getArea()); console.log("perimeter = "+square1.getPerimeter());
  • 33. Exercises for Interfaces/Classes 1) Define an interface IPerson that contains a first name, last name, and zip code 2) Create a Person2 class that implements IPerson: class Person2 implements IPerson { … } 3) Modify IPerson so that it contains an optional weight property
  • 34. IDEs for TypeScript WebStorm IDE (30-day trial) Visual Studio Code (free and cross-platform) TypEcs (TypeScript IDE for Eclipse): http://typecsdev.com/ http://definitelytyped.org/directory/tools.html
  • 35. TypeScript Versions • https://github.com/Microsoft/TypeScript/wiki/Ro admap • Navigate to website to see list of features for: • TS 2.1 • TS 2.0 • TS 1.9 • TS 1.8 • TS 1.7 • TS 1.6
  • 36. Recent/Upcoming Books and Training 1) HTML5 Canvas and CSS3 Graphics (2013) 2) jQuery, CSS3, and HTML5 for Mobile (2013) 3) HTML5 Pocket Primer (2013) 4) jQuery Pocket Primer (2013) 5) HTML5 Mobile Pocket Primer (2014) 6) D3 Pocket Primer (2015) 7) Python Pocket Primer (2015) 8) SVG Pocket Primer (2016) 9) CSS3 Pocket Primer (2016) 10) Angular 2 Pocket Primer (2016)