the advantage of developing with typescript
TRANSCRIPT
![Page 1: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/1.jpg)
Angular / Angular 2.0 The advantage of developing
with TypeScript
#angularconf15
http://2015.angularconf.it/
![Page 2: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/2.jpg)
Disclaimer
This presentation is CAT FREE!
No Animals (with the exception of some developers) Were Harmed during the creation of this work.
![Page 3: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/3.jpg)
Who Am I ?Alessandro Giorgetti
co-owner: SID s.r.l.co-founder: DotNetMarche, DevMarche
Facebook: https://www.facebook.com/giorgetti.alessandroTwitter: @a_giorgetti
LinkedIn: https://it.linkedin.com/in/giorgettialessandroE-mail: [email protected]
Blog: www.primordialcode.com
![Page 4: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/4.jpg)
Gimme the code!https://github.com/AGiorgetti/AngularConf2015
https://github.com/AGiorgetti/AngularConf2015_ng2
![Page 5: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/5.jpg)
How much productive are you when writing an Angular
application?
![Page 6: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/6.jpg)
Is it easy to maintain and refactor your Angular application?
![Page 7: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/7.jpg)
Are your tools supporting you properly?
![Page 8: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/8.jpg)
Can it be better?
![Page 9: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/9.jpg)
Agenda• TypeScript
a quick introduction, setup and usage
• Types, Interfaces and ClassesHelp us structuring the application!Help the tools provide us more information!
• Sounds good: show me some Angular code!Write an Angular app with TypeScript:
• Service• Controller• Directive
• Q. & A.
![Page 10: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/10.jpg)
TypeScriptIntroduction, setup and usage
![Page 11: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/11.jpg)
When your JavaScript app becomes big...• Lack of Code Structuring / Coherence:
• Many different style of writing JavaScript.• Lack of Object Oriented design paradigms and class based programming techniques. • 'New / Unusual' design patterns (prototypical inheritance, revealing module patterns etc...). • You need to define a code style guide. • You need to enforce that style guide: it needs discipline!
• No type checking!• You need more tests to catch trivial errors.• No way to ‘enforce’ code contracts or constraints.• Code is not self-documented: you NEED better documentation.
• Tooling isn’t good enough!• No (or very poor) code analysis.• No type checking.• Very poor refactoring support.• Intellisense ? Can you trust it ?
![Page 12: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/12.jpg)
More often than not…
JavaScript tools fail!
The good news: JavaScript is evolving! ES6* to the rescue!
* the problem is you cannot have full access to those feature right now! You'll have to wait... and ES5 will be out in the wild for quite some time anyway...
![Page 13: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/13.jpg)
TypeScript • It's an Open Source project from Microsoft Technologies.
• An attempt to 'fix' the missing parts of JavaScript.
• A Superset of JavaScript => JavaScript + Static Types (and Classes and Modules and more…).
• It uses ES6 syntax with Type Annotation and compiles to plain JavaScript (target: ES3, ES5, ES6).
• Any valid JavaScript application is also a TypeScript application.
![Page 14: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/14.jpg)
TypeScript Helps us to:
• Structure our code (interfaces, classes and modules).• Use object-oriented programming paradigms and techniques.• Enforce coding guidelines.
Enables a better Coding Experience:• Intellisense.• Syntax checking.• Code Analysis & Navigation.• Refactoring.• Documentation.
Gets us ready for Angular 2.0.
The best part of it: It's all a development time illusion!
![Page 15: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/15.jpg)
Tools can be improved!
Intellisense works (properly)! Helpful documentation!
Types Annotations!
![Page 16: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/16.jpg)
And help you spot errors!
Calling a function with wrong arguments?
Have you mistyped something?
![Page 17: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/17.jpg)
Code Navigation and Refactoring
Code Navigation: go to definition, find reference, etc…
Refactoring!
![Page 18: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/18.jpg)
Setup TypeScriptYou have several ways to install TypeScript (globally and locally): http://www.typescriptlang.org/#Download
![Page 19: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/19.jpg)
TSC - the TypeScript compiler
TSC is a source-to-source compiler (a transpiler).
There are lots of options that allow you to:• concatenate different files in a single output file.• generate sourcemaps.• generate module loading code (node.js or require.js).
tsc app.tsapp.ts app.js
![Page 20: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/20.jpg)
TSD - TypeScript Definition Files package manager
TypeScript Definition File (ambient declaration file)• .d.ts extension.• Allows the definition of strong types.• Provide type definition for external JavaScript libraries.
DefinitelyTyped (http://definitelytyped.org/):a community driven project on GitHub that tracks all of them.
TSD: a specialized package manager to look for definition files inside DefinitelyTyped repository.
![Page 21: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/21.jpg)
Types, Interfaces and ClassesSome quick words on these concepts
![Page 22: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/22.jpg)
Typesnumber, string, etc... all the primitive JavaScript Types.
any: I can be any type, disable the type checking!
void: I have no type at all (function return value)!
enum / const enum: define enumerated values.
<T>: casting! This is not a type conversion!
generics: great for code reuse! We can specify constraints if we want.
![Page 23: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/23.jpg)
InterfacesAn interface defines a contract in your code, the shape of an entity.
Interfaces can describe:• Objects• Functions• Arrays / Dictionaries• Hybrid Types ('things' that are both objects and functions)
Interfaces support:• Inheritance
They do not support accessors (get / set): you need to convert the 'property' to a 'getProperty()' function if you wanna give that readonly behavior
![Page 24: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/24.jpg)
ClassesClasses implement the behaviors of an entity, it brings the entity to life.
They have support for:• accessors (get, set) [ES5+]• modifiers: public, private, protected• constructor• inheritable• static properties• abstract (class & methods)• interface implementation
Classes also define Types, they have two sides:• instance side (the properties involved in structural type checking)• static side (constructor and static properties, not involved in the type checking)
![Page 25: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/25.jpg)
Structural Typing / Duck TypingInterface and Classe are used to define new Types!
The shape of an object matters!
Two different objects (interfaces, classes) that expose the same properties are considered compatible.
“This mean you can assign 'apples' to 'oranges' under specific conditions”.
![Page 26: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/26.jpg)
Show me the Code!
Write a simple ‘ToDo List’ application that interact with an external service.
(let’s have a side by side comparison)
![Page 27: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/27.jpg)
Angular favors:• Separation of Concerns.• Code Structuring (module, service, controller,
directive).
TypeScript is all about:• Code Structuring (interface, class, namespace,
module).• Better tooling / development experience.
![Page 28: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/28.jpg)
Angular - concepts TypeScript – best implemented with
Business Entities interface, class
Service interface, class
Controller class (interface)
Directive function
![Page 29: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/29.jpg)
Service [implement them using a class]
![Page 30: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/30.jpg)
Service [Class declaration and constructor]
A generic ‘function’ becomes a ‘class’
An initialization function becomes the constructor
Dependency injection is specified with a static property
Usage of arrow functions to properly manage the ‘this’
![Page 31: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/31.jpg)
Service [define member functions]
No need to use the ‘function’ keyword.No need to specify ‘this.’: functions already belongs to the class.
1) Creates an ‘instance’ function.
2) Creates a ‘prototype’ function.
1
2
![Page 32: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/32.jpg)
The ‘This’The 'this': most of the times it represents the instance of the class itself (like in C#).
The 'this' has a different meaning in function expression and when using the 'arrow syntax':
• function() { … }: this act exactly as expected in strict mode (it can be undefined or whatever it was when entering the function execution context).
• () => { … }: this always refers to the class instance.
Composition / Encapsulation patterns: don't mess up with the this! Always delegate the function call properly, that is: call the function on its original object rather than assigning the pointer to the function to another variable!
![Page 33: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/33.jpg)
In terms of dev experience…
![Page 34: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/34.jpg)
Controller [mplement them using a class]
![Page 35: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/35.jpg)
Directive [implement them using a function…]
![Page 36: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/36.jpg)
Directive […or a class]
![Page 37: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/37.jpg)
Angular 2.0
• Built with TypeScript.
• Heavy use of Decorators to annotate objects.
• Except for some ‘infrastructure’ code needed by Angular 2.0, there’s not much difference in how you implement Services and Components using TypeScript.
![Page 38: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/38.jpg)
Decorators (ES7 proposal)Decorators make it possible to annotate and modify classes and properties at design time.
A decorator is:• an expression• that evaluates to a function• that takes the target, name, and property descriptor as arguments• and optionally returns a property descriptor to install on the target object
In TypeScript we have 4 types of decorators:• ClassDecorator• MethodDecorator • PropertyDecorator • ParameterDecorator
![Page 39: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/39.jpg)
Service / Injectable
No difference in how the service is built, except some api calls!
Angular 1.x Angular 2.0
![Page 40: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/40.jpg)
Component (controller & directive)Angular 1.x Angular 2.0
![Page 41: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/41.jpg)
Thanks All!
I hope you enjoyed the session!
Let’s stay in touch!
![Page 42: The advantage of developing with TypeScript](https://reader031.vdocuments.site/reader031/viewer/2022030316/58729d391a28ab07208b4f93/html5/thumbnails/42.jpg)
Q. & A.
Ask me something!