SlideShare a Scribd company logo
1 of 14
Download to read offline
TYPESCRIPT
2
TYPESCRIPTTYPESCRIPT
Contents
Introduction............................................................................................................................................................. 3
What is this e-book for?........................................................................................................................................ 3
What is TypeScript?................................................................................................................................................ 4
Typescript Installation........................................................................................................................................... 4
Why Typescript?...................................................................................................................................................... 4
Types in TS............................................................................................................................................................... 5
Support to duck typing.......................................................................................................................................... 5
Use of Jquery in TS................................................................................................................................................. 6
Handling Undefined .............................................................................................................................................. 6
This Keyword........................................................................................................................................................... 6
Creation of classes................................................................................................................................................. 7
Single Inheritance in TypeScript.......................................................................................................................... 7
Static Keyword......................................................................................................................................................... 7
Access modifiers..................................................................................................................................................... 8
Abstract keyword.................................................................................................................................................... 8
Property Initializer (ES 7 feature)........................................................................................................................ 8
Super keyword........................................................................................................................................................ 8
Arrow functions....................................................................................................................................................... 9
Inheritance & this keyword.................................................................................................................................. 9
Rest parameters..................................................................................................................................................... 9
Let keyword.............................................................................................................................................................. 9
Const keyword......................................................................................................................................................... 9
Deep Immutability in TypeScript......................................................................................................................... 10
Destructuring in TypeScript................................................................................................................................. 10
Array destructuring................................................................................................................................................ 10
Array destructuring with rest............................................................................................................................... 10
Array destructuring with ignores........................................................................................................................ 11
Forโ€ฆ.Of keyword..................................................................................................................................................... 11
Template Strings .................................................................................................................................................... 11
Spread operator..................................................................................................................................................... 12
Promise..................................................................................................................................................................... 12
Creating a promise................................................................................................................................................. 12
Tip............................................................................................................................................................................... 12
Chainability of promise......................................................................................................................................... 13
Best practices ......................................................................................................................................................... 13
Conclusion................................................................................................................................................................ 13
3
TYPESCRIPT
Introduction
Ever Since web browsers and internet was a part of our civilisation, Javascript ruled the part of adding interactions
to our web applications/web sites. Over the years, as days pass by, Javascript (nick-named โ€˜JSโ€™) became versatile.
With technologies like NodeJS, ElectronJS and third party component integrations from Facebook, twitter and so
on, JS now stands tall. NodeJS calls itself server side javascript, that means we develop server side scalable network
components/ web apis with javascript. It never stops there, JS is now distributable. We use Facebookโ€™s share icon by
integrating their JS library along with some custom html tags. JS is now used to build large-scale applications today.
When we say large-scale applications, there are concepts like reusability and maintainability concern us. JS still is
interpreted, not compiled language. ECMAScript is the subset of Javascript. ECMAScript updates its standards for
new user experiences. ECMAScript 5 and above has lots of new features introduced. Javascript tries to adapt to the
latest standards from ECMAScript but that now takes time. A team of engineers at Microsoft understood some of the
maintainability and scalability issues with Javascript and would like to resolve it by introducing Typescript.
Typescript is not a replacement for Javascript but is a superset of Javascript. The code that was built with JS will run
in Typescript too. Typescript is written following ES6 and above features. We will describe what it is in the following
pages.
What is this e-book for?
We saw an increase in the open-source community on Typescript usage. Some of the worldโ€™s famous open source js
libraries now use Typescript. One of the finest examples is the protobuf.js library (https://github.com/dcodeIO/pro-
tobuf.js). It is the next generation to our rest apis designed at Google for faster data access without JSON. Angular
recommends typescript for its developers with its latest versions. There are more TypeScript repositories in github
on search than Javascript (April 01,2018).
Typescript
ES6
ES5
โ€ข	 types
โ€ข	 annotations
โ€ข	 classes
โ€ข	 modules
4
TYPESCRIPT
(screen grab of repositories in github from url https://github.com/search?utf8=%E2%9C%93&q=typescript)
This e-book neither advocates JS lovers to convert to TS nor TS lovers to feel superior. Softcrylic uses TypeScript in
AutomateOn and tried for some components of TapestryKPI. I would like to recommend you love this new standard
of writing Javascript code. Below are some of the new items in TypeScript that makes this โ€˜new programming lan-
guageโ€™ interesting.
What is TypeScript?
TypeScript, simply, is a programming language, but not to replace Javascript. It is written based on ECMAScript stan-
dards 5 and above, calling it a superset of TypeScript.
Typescript compiles into JavaScript. In order to execute it, we need to have a
a.	 Typescript compiler,
a.	 Typescript editor (VS Code, Visual Studio 2013 updated and above, sublime text, atom or any editor of your 	
choice)
a.	 To install typescript compiler, we can use npm/sublime plugin and so on
Typescript Installation
We can run typescript either on a browser or a server.
Typescript is available in npm and can be downloaded by this command globally in your system
For OSX it has to be
Why Typescript?
a.	 JS never had variable types
The above syntax works, but in large systems, if we perform any operations on data, the output differs.
5
TYPESCRIPT
Typescript checks types at compile time. It identifies type mismatch early.
Provides classes and module to follow an object-oriented standard for projects that are highly scalable. When we
deal with multiple codebases, typescript helps a lot.
Types in TS
TypeScript supports the following data types
a.	 Boolean -> var status:Boolean = false;
b.	 Number -> var a:number=123;
c.	 String -> var s:string=โ€123โ€;
d.	 Array -> var list:number[] =[1,2,3];
e.	 Enum -> enum Color{Red, Green, Blue};
f.	 Any -> var notSure:any;notSure=123;notsure=โ€456โ€;
g.	 Void -> function Do Something:void(){}
Even if data type is not specified it assigns a data type based on the values we assign
Support to duck typing
In JS, types are structural and hence duck typing is accepted. We can pass additional parameters as types and it is all
right.
6
TYPESCRIPT
Use of Jquery in TS
Consider the jquery code we will use for a selector.
The above line of code will throw error saying $ is not defined while Jquery file is not referenced in JS. In TypeScript $
throws error when not declared. We have to try
Or
Protects you from portion of JS that never worked
Handling Undefined
It is important for us to check a property or a variable undefined or null in strict mode. If you used component based
frameworks strict mode is mandatory & automatically added.
This Keyword
The keyword โ€˜thisโ€™ is called a calling context
//return the foo method instance
7
TYPESCRIPT
Creation of classes
Classes provide structural abstraction.
Single Inheritance in TypeScript
TS supports single inheritance using the extends keyword.
Static Keyword
TS introduces static keywords that can be used on classes.
8
TYPESCRIPT
Access modifiers
Accessible on Public Protected Private
Class Yes Yes Yes
Class children Yes Yes No
Class instances Yes No No
Abstract keyword
Property Initializer (ES 7 feature)
Super keyword
Used to initialize an object in the base class method.
9
TYPESCRIPT
Arrow functions
๏ƒ  is thin arrow.
๏ƒจ Is fat arrow functions (used) or lambda functions
Inheritance & this keyword
We canโ€™t use โ€˜superโ€™ keyword without โ€˜thisโ€™ keyword. You can create a copy & use it.
Rest parameters
Rest parameters (denoted by โ€ฆ..argument Name for last argument) allow you to quickly accept multiple arguments
in your function and get them as an array.
Let keyword
Var keyword in JS is function scoped.
This is why ES6 introduces โ€˜letโ€™ keyword
Use let if you want a variable to be block scoped loop failures.
Const keyword
๏ƒจ Declare constant with โ€˜constโ€™ keyword.
๏ƒจ Must be initialized during declaration
10
TYPESCRIPT
Block scoped
Deep Immutability in TypeScript
Destructuring in TypeScript
Breaking up the structure
Object destructuring Array destructuring
If assign an extended variable to a new variable name you can do the following.
Array destructuring
Array destructuring with rest
11
TYPESCRIPT
Array destructuring with ignores
Forโ€ฆ.Of keyword
Disadvantages of for in
Forโ€ฆof exists in TS (ES6)
	
Template Strings
Syntactically these are strings that use backticks (i.e.) instead of angle (โ€˜) or double (โ€œ) quotes.
a.	 String interpolation
b.	 Multiline Strings
c.	 Tagged templates.
12
TYPESCRIPT
Spread operator
Main objective is to spread the objects of an array.
In JS we had to us function.prototype.apply()
Promise
Modern JS engines have this and easily polyfilled.
The main motivation behind promises is to bring synchronous style and error handling to Async/Callback style code.
When we are using a callback it should follow the below rules while in call back.
a.	 Never call the callback twice
b.	 Never throw an error
Creating a promise
A promise can be either
a.	 Pending,
b.	 Resolved,
c.	 Rejected
	
Tip
โ€ข	 Creating an already resolved promise: promise.resolve (result)
โ€ข	 Creating an already rejected promise: promise.reject (error)
13
TYPESCRIPT
Chainability of promise
We can chain promises using the โ€˜thenโ€™ function
There are 2 types of declaration spaces in typescript
Type declaration Variable declaration
Best practices
โ€ข	 Functions variables should be camel case
โ€ข	 Use pascal case for classes, interface
โ€ข	 Interface members should be camel case
โ€ข	 Do not use the prefix I (unconventional lib.d.ts defines important interfaces without an I)
โ€ข	 Namespaces should be pascal case, enum
โ€ข	 Donโ€™t make variable undefined but nullable
โ€ข	 Use โ€˜return undefinedโ€™ instead of null if itโ€™s a part of A/I or conventional (Nodys error is null by default)
โ€ข	 Use undefined for next level truthy or falsy.
โ€ข	 Use error 1=undefined & not error
โ€ข	 Space b4 type const foo: string=โ€helloโ€;
โ€ข	 Prefer single quotes
โ€ข	 Prefer backticks if possible
โ€ข	 Donโ€™t use tabs but 2 spaces
โ€ข	 Use semicolon
Conclusion
Typescript has introduced a new way of writing javascript applications at large scale. With the power of ES6 and above,
the language looks simple, object-oriented and easy to learn. The new features are more, but this e-book describes
some of the important syntax Typescript offered to solve real-world problems JS was facing for years. TypeScript would
be worth a try for large-scale applications.
14
TYPESCRIPT
About Author
Krishnanand Sivaraj is an inquisitive Software Techie and a continuous learner who loves archi-
tecture, debugging and knowledge sharing. Working as a โ€œLead Engineerโ€ at Softcrylic, he actively
blogs, attends tech-meetups to keep himself updated with futuristic trends.
ยฉ 2018 Softcrylic. All rights reserved. Softcrylic is a registered trademark of Softcrylic, LLC. The above content is proprietary to SoftcrylicApplications
of five common practices
About Softcrylic
Softcrylic helps organizations navigate and execute the path of Digital Transformation through IT solutions and services in
a variety of technical disciplines including Software Development, Test Engineering, Data and Analytics. Since 2000, Soft-
crylic has worked with both start-ups and Fortune 500 organizations to help make their company goals a reality.
Get in Touch with us
Email: info@softcylic.com
Phone: +1 609 452 7638
For more information, visit https://softcrylic.com

More Related Content

Similar to TypeScipt - Get Started

Log4 C Developers Guide
Log4 C Developers GuideLog4 C Developers Guide
Log4 C Developers Guide
gzm55
ย 
Project
ProjectProject
Project
Xu Liu
ย 
How To Do Everything With JavaScript
How To Do Everything With JavaScriptHow To Do Everything With JavaScript
How To Do Everything With JavaScript
Azharul Haque Shohan
ย 

Similar to TypeScipt - Get Started (20)

Intermediate python
Intermediate pythonIntermediate python
Intermediate python
ย 
Thesis
ThesisThesis
Thesis
ย 
James Baxley - Statically typing your GraphQL app
James Baxley - Statically typing your GraphQL appJames Baxley - Statically typing your GraphQL app
James Baxley - Statically typing your GraphQL app
ย 
An Introduction to TypeScript
An Introduction to TypeScriptAn Introduction to TypeScript
An Introduction to TypeScript
ย 
salesforce_apex_developer_guide
salesforce_apex_developer_guidesalesforce_apex_developer_guide
salesforce_apex_developer_guide
ย 
Log4 C Developers Guide
Log4 C Developers GuideLog4 C Developers Guide
Log4 C Developers Guide
ย 
In designcs5 scripting tutorial
In designcs5 scripting tutorialIn designcs5 scripting tutorial
In designcs5 scripting tutorial
ย 
An Introduction to TypeScript: Definition, History, and Key Features
An Introduction to TypeScript: Definition, History, and Key FeaturesAn Introduction to TypeScript: Definition, History, and Key Features
An Introduction to TypeScript: Definition, History, and Key Features
ย 
What is TypeScript? It's Definition, History And Features
What is TypeScript? It's Definition, History And FeaturesWhat is TypeScript? It's Definition, History And Features
What is TypeScript? It's Definition, History And Features
ย 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
ย 
Migrating Web SDK from JS to TS
Migrating Web SDK from JS to TSMigrating Web SDK from JS to TS
Migrating Web SDK from JS to TS
ย 
Type script = javascript (alomst) done right
Type script = javascript (alomst) done rightType script = javascript (alomst) done right
Type script = javascript (alomst) done right
ย 
Systems se
Systems seSystems se
Systems se
ย 
Project
ProjectProject
Project
ย 
Rspec tutorial
Rspec tutorialRspec tutorial
Rspec tutorial
ย 
Javascript Deofuscation A manual Approach
Javascript Deofuscation A manual ApproachJavascript Deofuscation A manual Approach
Javascript Deofuscation A manual Approach
ย 
React js basics
React js basicsReact js basics
React js basics
ย 
How To Do Everything With JavaScript
How To Do Everything With JavaScriptHow To Do Everything With JavaScript
How To Do Everything With JavaScript
ย 
Scala's evolving ecosystem- Introduction to Scala.js
Scala's evolving ecosystem- Introduction to Scala.jsScala's evolving ecosystem- Introduction to Scala.js
Scala's evolving ecosystem- Introduction to Scala.js
ย 
Asm.js introduction
Asm.js introductionAsm.js introduction
Asm.js introduction
ย 

Recently uploaded

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
SUHANI PANDEY
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
ย 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
ย 

Recently uploaded (20)

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
ย 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ย 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
ย 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
ย 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
ย 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
ย 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
ย 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
ย 

TypeScipt - Get Started

  • 2. 2 TYPESCRIPTTYPESCRIPT Contents Introduction............................................................................................................................................................. 3 What is this e-book for?........................................................................................................................................ 3 What is TypeScript?................................................................................................................................................ 4 Typescript Installation........................................................................................................................................... 4 Why Typescript?...................................................................................................................................................... 4 Types in TS............................................................................................................................................................... 5 Support to duck typing.......................................................................................................................................... 5 Use of Jquery in TS................................................................................................................................................. 6 Handling Undefined .............................................................................................................................................. 6 This Keyword........................................................................................................................................................... 6 Creation of classes................................................................................................................................................. 7 Single Inheritance in TypeScript.......................................................................................................................... 7 Static Keyword......................................................................................................................................................... 7 Access modifiers..................................................................................................................................................... 8 Abstract keyword.................................................................................................................................................... 8 Property Initializer (ES 7 feature)........................................................................................................................ 8 Super keyword........................................................................................................................................................ 8 Arrow functions....................................................................................................................................................... 9 Inheritance & this keyword.................................................................................................................................. 9 Rest parameters..................................................................................................................................................... 9 Let keyword.............................................................................................................................................................. 9 Const keyword......................................................................................................................................................... 9 Deep Immutability in TypeScript......................................................................................................................... 10 Destructuring in TypeScript................................................................................................................................. 10 Array destructuring................................................................................................................................................ 10 Array destructuring with rest............................................................................................................................... 10 Array destructuring with ignores........................................................................................................................ 11 Forโ€ฆ.Of keyword..................................................................................................................................................... 11 Template Strings .................................................................................................................................................... 11 Spread operator..................................................................................................................................................... 12 Promise..................................................................................................................................................................... 12 Creating a promise................................................................................................................................................. 12 Tip............................................................................................................................................................................... 12 Chainability of promise......................................................................................................................................... 13 Best practices ......................................................................................................................................................... 13 Conclusion................................................................................................................................................................ 13
  • 3. 3 TYPESCRIPT Introduction Ever Since web browsers and internet was a part of our civilisation, Javascript ruled the part of adding interactions to our web applications/web sites. Over the years, as days pass by, Javascript (nick-named โ€˜JSโ€™) became versatile. With technologies like NodeJS, ElectronJS and third party component integrations from Facebook, twitter and so on, JS now stands tall. NodeJS calls itself server side javascript, that means we develop server side scalable network components/ web apis with javascript. It never stops there, JS is now distributable. We use Facebookโ€™s share icon by integrating their JS library along with some custom html tags. JS is now used to build large-scale applications today. When we say large-scale applications, there are concepts like reusability and maintainability concern us. JS still is interpreted, not compiled language. ECMAScript is the subset of Javascript. ECMAScript updates its standards for new user experiences. ECMAScript 5 and above has lots of new features introduced. Javascript tries to adapt to the latest standards from ECMAScript but that now takes time. A team of engineers at Microsoft understood some of the maintainability and scalability issues with Javascript and would like to resolve it by introducing Typescript. Typescript is not a replacement for Javascript but is a superset of Javascript. The code that was built with JS will run in Typescript too. Typescript is written following ES6 and above features. We will describe what it is in the following pages. What is this e-book for? We saw an increase in the open-source community on Typescript usage. Some of the worldโ€™s famous open source js libraries now use Typescript. One of the finest examples is the protobuf.js library (https://github.com/dcodeIO/pro- tobuf.js). It is the next generation to our rest apis designed at Google for faster data access without JSON. Angular recommends typescript for its developers with its latest versions. There are more TypeScript repositories in github on search than Javascript (April 01,2018). Typescript ES6 ES5 โ€ข types โ€ข annotations โ€ข classes โ€ข modules
  • 4. 4 TYPESCRIPT (screen grab of repositories in github from url https://github.com/search?utf8=%E2%9C%93&q=typescript) This e-book neither advocates JS lovers to convert to TS nor TS lovers to feel superior. Softcrylic uses TypeScript in AutomateOn and tried for some components of TapestryKPI. I would like to recommend you love this new standard of writing Javascript code. Below are some of the new items in TypeScript that makes this โ€˜new programming lan- guageโ€™ interesting. What is TypeScript? TypeScript, simply, is a programming language, but not to replace Javascript. It is written based on ECMAScript stan- dards 5 and above, calling it a superset of TypeScript. Typescript compiles into JavaScript. In order to execute it, we need to have a a. Typescript compiler, a. Typescript editor (VS Code, Visual Studio 2013 updated and above, sublime text, atom or any editor of your choice) a. To install typescript compiler, we can use npm/sublime plugin and so on Typescript Installation We can run typescript either on a browser or a server. Typescript is available in npm and can be downloaded by this command globally in your system For OSX it has to be Why Typescript? a. JS never had variable types The above syntax works, but in large systems, if we perform any operations on data, the output differs.
  • 5. 5 TYPESCRIPT Typescript checks types at compile time. It identifies type mismatch early. Provides classes and module to follow an object-oriented standard for projects that are highly scalable. When we deal with multiple codebases, typescript helps a lot. Types in TS TypeScript supports the following data types a. Boolean -> var status:Boolean = false; b. Number -> var a:number=123; c. String -> var s:string=โ€123โ€; d. Array -> var list:number[] =[1,2,3]; e. Enum -> enum Color{Red, Green, Blue}; f. Any -> var notSure:any;notSure=123;notsure=โ€456โ€; g. Void -> function Do Something:void(){} Even if data type is not specified it assigns a data type based on the values we assign Support to duck typing In JS, types are structural and hence duck typing is accepted. We can pass additional parameters as types and it is all right.
  • 6. 6 TYPESCRIPT Use of Jquery in TS Consider the jquery code we will use for a selector. The above line of code will throw error saying $ is not defined while Jquery file is not referenced in JS. In TypeScript $ throws error when not declared. We have to try Or Protects you from portion of JS that never worked Handling Undefined It is important for us to check a property or a variable undefined or null in strict mode. If you used component based frameworks strict mode is mandatory & automatically added. This Keyword The keyword โ€˜thisโ€™ is called a calling context //return the foo method instance
  • 7. 7 TYPESCRIPT Creation of classes Classes provide structural abstraction. Single Inheritance in TypeScript TS supports single inheritance using the extends keyword. Static Keyword TS introduces static keywords that can be used on classes.
  • 8. 8 TYPESCRIPT Access modifiers Accessible on Public Protected Private Class Yes Yes Yes Class children Yes Yes No Class instances Yes No No Abstract keyword Property Initializer (ES 7 feature) Super keyword Used to initialize an object in the base class method.
  • 9. 9 TYPESCRIPT Arrow functions ๏ƒ  is thin arrow. ๏ƒจ Is fat arrow functions (used) or lambda functions Inheritance & this keyword We canโ€™t use โ€˜superโ€™ keyword without โ€˜thisโ€™ keyword. You can create a copy & use it. Rest parameters Rest parameters (denoted by โ€ฆ..argument Name for last argument) allow you to quickly accept multiple arguments in your function and get them as an array. Let keyword Var keyword in JS is function scoped. This is why ES6 introduces โ€˜letโ€™ keyword Use let if you want a variable to be block scoped loop failures. Const keyword ๏ƒจ Declare constant with โ€˜constโ€™ keyword. ๏ƒจ Must be initialized during declaration
  • 10. 10 TYPESCRIPT Block scoped Deep Immutability in TypeScript Destructuring in TypeScript Breaking up the structure Object destructuring Array destructuring If assign an extended variable to a new variable name you can do the following. Array destructuring Array destructuring with rest
  • 11. 11 TYPESCRIPT Array destructuring with ignores Forโ€ฆ.Of keyword Disadvantages of for in Forโ€ฆof exists in TS (ES6) Template Strings Syntactically these are strings that use backticks (i.e.) instead of angle (โ€˜) or double (โ€œ) quotes. a. String interpolation b. Multiline Strings c. Tagged templates.
  • 12. 12 TYPESCRIPT Spread operator Main objective is to spread the objects of an array. In JS we had to us function.prototype.apply() Promise Modern JS engines have this and easily polyfilled. The main motivation behind promises is to bring synchronous style and error handling to Async/Callback style code. When we are using a callback it should follow the below rules while in call back. a. Never call the callback twice b. Never throw an error Creating a promise A promise can be either a. Pending, b. Resolved, c. Rejected Tip โ€ข Creating an already resolved promise: promise.resolve (result) โ€ข Creating an already rejected promise: promise.reject (error)
  • 13. 13 TYPESCRIPT Chainability of promise We can chain promises using the โ€˜thenโ€™ function There are 2 types of declaration spaces in typescript Type declaration Variable declaration Best practices โ€ข Functions variables should be camel case โ€ข Use pascal case for classes, interface โ€ข Interface members should be camel case โ€ข Do not use the prefix I (unconventional lib.d.ts defines important interfaces without an I) โ€ข Namespaces should be pascal case, enum โ€ข Donโ€™t make variable undefined but nullable โ€ข Use โ€˜return undefinedโ€™ instead of null if itโ€™s a part of A/I or conventional (Nodys error is null by default) โ€ข Use undefined for next level truthy or falsy. โ€ข Use error 1=undefined & not error โ€ข Space b4 type const foo: string=โ€helloโ€; โ€ข Prefer single quotes โ€ข Prefer backticks if possible โ€ข Donโ€™t use tabs but 2 spaces โ€ข Use semicolon Conclusion Typescript has introduced a new way of writing javascript applications at large scale. With the power of ES6 and above, the language looks simple, object-oriented and easy to learn. The new features are more, but this e-book describes some of the important syntax Typescript offered to solve real-world problems JS was facing for years. TypeScript would be worth a try for large-scale applications.
  • 14. 14 TYPESCRIPT About Author Krishnanand Sivaraj is an inquisitive Software Techie and a continuous learner who loves archi- tecture, debugging and knowledge sharing. Working as a โ€œLead Engineerโ€ at Softcrylic, he actively blogs, attends tech-meetups to keep himself updated with futuristic trends. ยฉ 2018 Softcrylic. All rights reserved. Softcrylic is a registered trademark of Softcrylic, LLC. The above content is proprietary to SoftcrylicApplications of five common practices About Softcrylic Softcrylic helps organizations navigate and execute the path of Digital Transformation through IT solutions and services in a variety of technical disciplines including Software Development, Test Engineering, Data and Analytics. Since 2000, Soft- crylic has worked with both start-ups and Fortune 500 organizations to help make their company goals a reality. Get in Touch with us Email: info@softcylic.com Phone: +1 609 452 7638 For more information, visit https://softcrylic.com