Slide 1

Slide 1 text

Perfect Match NodeJs & Typescript 63º Meeting @ LISBON 24/09/16 Marco Talento @Talento90

Slide 2

Slide 2 text

Sponsors “GOLD” Twitter: @PTMicrosoft http://www.microsoft.com/portugal

Slide 3

Slide 3 text

Sponsors “Silver”

Slide 4

Slide 4 text

Sponsors “Bronze”

Slide 5

Slide 5 text

Presentation Rating http://bit.ly/netponto-aval-63

Slide 6

Slide 6 text

Me, Myself & I ● Software Engineer at NOS Inovação (BoldInt Consultant) ● Sharednode Founder ● Open Source Contributor Github: https://github.com/Talento90 Twitter: https://twitter.com/Talento90 LinkedIn: https://www.linkedin.com/in/marcotalento Blog: http://sharednode.org/author/mtalento90/

Slide 7

Slide 7 text

Agenda ● Introduction ● TypeScript ● Tools & IDE’s ● Debugging ● Demo ● Conclusion & Thoughts

Slide 8

Slide 8 text

Introduction

Slide 9

Slide 9 text

JavaScript ● Dynamic and interpreted ● Simple and easy to learn ● Cross Platform ● Runs Everywhere (Browser, Server Side) ● Big Community (Most popular language on Github) ● Continuous Evolution (ES5, ES6, ES7…)

Slide 10

Slide 10 text

Node.js ● Node.js was originally written in 2009 by Ryan Dahl. ● Community Driven ● Used in many different applications (Web Dev, IOT, Real Time Apps) ● Adopted by big companies (Netflix, Microsoft, Uber, etc...)

Slide 11

Slide 11 text

What is Node.js? ● "Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient." - Node.Js Site ● "Node.js is an open-source, cross-platform runtime environment for developing server-side web applications." - Wikipedia ● "Node.js is a platform that runs on top of V8 Google Engine and gives the possibility to build scalable server side applications using JavaScript." - Marco Talento

Slide 12

Slide 12 text

How it works? http://www.codingeek.com/tutorials/nodejs/is-nodejs-single-threaded/

Slide 13

Slide 13 text

Http Server (JavaScript)

Slide 14

Slide 14 text

JavaScript Problems ● Lack of type safety ● Easy to create “spaghetti” code ● Hard to scale

Slide 15

Slide 15 text

Solution?

Slide 16

Slide 16 text

TypeScript ● TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ● Includes all features of ES6 and much more: types, interfaces, generics, etc... https://johnpapa.net/es5-es2015-typescript/

Slide 17

Slide 17 text

Types Prevent Bugs

Slide 18

Slide 18 text

Definitions ● Definitions give intellisense and module typification. ● TypeScript definitions are suffixed by “.d.ts” ● Maintainable by community ● To reference the modules it’s necessary to include the following line on the top of each TS file: /// ○ Typings it’s a tool to manage definitions

Slide 19

Slide 19 text

Typings Typings is a tool to manage and install TypeScript definitions. It uses typings.json to handle dependencies. Github: https://github.com/typings/typings ● Install typings: npm install typings --global ● Search for typings: typings search hapi ● Install Typings: typings install dt~hapi --global --save

Slide 20

Slide 20 text

Example

Slide 21

Slide 21 text

Definition doesn’t exist? LAZY: Ignore the TS errors, the generated code will work! Perfect: Create and contribute to the community ○ http://definitelytyped.org/guides/contributing.html

Slide 22

Slide 22 text

Typescript (BAD) ● Lack of definitions ● Additional steps (compilation, typings, etc...)

Slide 23

Slide 23 text

Typescript (GOOD) ● Easy to learn ● Full OOP (interfaces, abstract classes, etc...) ● TYPES prevents bugs ● Code Scalability ● Open Source ● Supported by Microsoft

Slide 24

Slide 24 text

How to “TypeScript”? 1. Install: npm install -g typescript 2. Create file app.ts 3. Compile using the command line: tsc app.ts 4. Run the result: node app.js

Slide 25

Slide 25 text

Compilation Result

Slide 26

Slide 26 text

Compiler Configuration Use tsconfig.json to specify the compiler options. Options: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Slide 27

Slide 27 text

Example (tsconfig.json)

Slide 28

Slide 28 text

TypeScript 2.0 ● Improve Control Flow Analyzed Types ● The readonly properties ● Simplified declaration file (npm install --save @types/lodash) ● Tsconfig supports glob patterns (Include) ● More: https://github.com/Microsoft/TypeScript/wiki/Roadmap#20

Slide 29

Slide 29 text

Node.js & TypeScript

Slide 30

Slide 30 text

Using TypeScript 1. Install nodejs typings 2. Create a tsconfig.json and add module “commonjs” 3. Reference definitions 4. Ready to code your application

Slide 31

Slide 31 text

Http Server (TypeScript)

Slide 32

Slide 32 text

Project Structure ● Build - Compilation Result (JavaScript Files) ● Src - Application source code ● Test - Application Tests ● Typings - Contains all the definitions ● Gulpfile.js - Gulp Tasks (compile, clean, tests, etc…) ● Tsconfig.json - compiler options ● Tslint.json - TypeScript linter rules

Slide 33

Slide 33 text

Tools & IDE’s

Slide 34

Slide 34 text

IDE & Tools TSLint - A Linter for the TypeScript language. Typings - The TypeScript Definition Manager. IDE’s ● VS Code (https://code.visualstudio.com/) ● Sublime (https://github.com/Microsoft/TypeScript-Sublime-Plugin) ● Atom (https://atom.io/packages/atom-typescript)

Slide 35

Slide 35 text

Debugging

Slide 36

Slide 36 text

Debugging ● Source Maps ○ Files that contains the mapping between the TS and JS ○ Debuggers know how to interpret this files ○ Suffixed by filename.map.js { "version" : 3, "file" : "database.js", "sourceRoot" : "", "sources" : ["../../src/database.ts"], "mappings" : ";AAAA,MAAY,QAAQ,WAAM,UAAU,CAAC, ..." - BASE64 VLQ }

Slide 37

Slide 37 text

Node Inspector ● Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools (formerly WebKit Web Inspector). ● The project maintenance and support is sponsored by StrongLoop. Install: npm install -g node-inspector Run: node-debug app.js

Slide 38

Slide 38 text

Node Inspector Interface

Slide 39

Slide 39 text

DEMO Let’s put everything together!

Slide 40

Slide 40 text

Conclusion & Thoughts

Slide 41

Slide 41 text

Conclusions & Thoughts ● Easier to write scalable & reliable code ● JavaScript tools also works with TypeScript ● Improve developers productivity ● Can be used everywhere (browser, node.js) ● TypeScript it’s growing fast and adopted by big companies

Slide 42

Slide 42 text

References ● https://nodesource.com/blog/needtonode-recap-best-practices-using-type script-with-node-js/ ● http://jiajizhou.com/2015/04/15/nodejs-profiling-tools.html ● http://www.typescriptlang.org/ ● https://www.gitbook.com/book/basarat/typescript/details ● http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/

Slide 43

Slide 43 text

Questions?

Slide 44

Slide 44 text

Thank you :)