Slide 1

Slide 1 text

How To Combine Types In Your Node.js Server Tamar Twena-Stern

Slide 2

Slide 2 text

Tamar Twena-Stern • Software Engineer - manager and architect • Architect @PaloAltoNetworks • Was a CTO of my own startup • Passionate about Node.js ! • Twitter: @SternTwena

Slide 3

Slide 3 text

Tamar Twena-Stern • On Maternity Leave • Have 3 kids • Loves to play my violin • Javascript Israel community leader

Slide 4

Slide 4 text

Strongly Typed Languages • Use of programming language types in order to • Capture invariants of the code • Ensure its correctness • Definitely exclude certain classes of programming errors. • Stricter typing rules at compile time • Errors and exceptions are more likely to happen during compilation. • Rules affect variable assignment, return values and function calling.

Slide 5

Slide 5 text

JavaScript - Dynamically Typed language • No typing declarations • Runtime error checking • No types information • No return statement

Slide 6

Slide 6 text

Problems

Slide 7

Slide 7 text

Function Return

Slide 8

Slide 8 text

Lets Look At Read File

Slide 9

Slide 9 text

Third Party Authentication With Enum

Slide 10

Slide 10 text

Demo - Simple Function Call, No Types

Slide 11

Slide 11 text

Can You Use This API ?

Slide 12

Slide 12 text

Problem Summary • Hard to use existing APIs • Inconsistent types in your code • Type casting is hell • Hard for other developers to read the code and work on top of it

Slide 13

Slide 13 text

TypeScript

Slide 14

Slide 14 text

TypeScript • Superset of JavaScript • Transpiles to JavaScript • Adds optional static typing • Every JavaScript program is a valid TypeScript program • Can be used to develop Node.js servers

Slide 15

Slide 15 text

Transpiles To JavaScript Deploying and running JavaScript files TypeScript compiler create JavaScript files in build folder TypeScript source files

Slide 16

Slide 16 text

Demo - Simple Node.js TypeScript Server

Slide 17

Slide 17 text

Working With TypeScript In Node.js

Slide 18

Slide 18 text

TypeScript Compiler • Standard Microsoft TypeScript compiler • Using the command tsc : • Transpiles all .ts files • Create a build folder with JavaScript source files • Running, debugging and deployment on the build folder

Slide 19

Slide 19 text

Ts-node • An executable that we are running • Registers the TypeScript compiler for the relevant TypeScript file extensions. • Transpiling relevant extensions on the fly

Slide 20

Slide 20 text

Flow

Slide 21

Slide 21 text

Flow • Open source package • developed by Facebook • Static type checker for JavaScript • Works with babel compiler • JavaScript files with flow annotations

Slide 22

Slide 22 text

Flow Compiles With Babel Deploying and running JavaScript files Babel compiler create JavaScript files in build folder JavaScript source files with flow annotation

Slide 23

Slide 23 text

Simple Node Server With Flow

Slide 24

Slide 24 text

Lets Vote For Using Types In Node!

Slide 25

Slide 25 text

Basic Usage • Annotate your code with types • Catch errors during “compile” time • No types = plane JavaScript.

Slide 26

Slide 26 text

Demo - Basic Usage

Slide 27

Slide 27 text

Ease Of Development • Ability to Relate objects to their type origin in the files • Ability to auto complete according to the type origin • Ability to relate type to the file origin • Speeding up development in complex projects

Slide 28

Slide 28 text

Demo - Type Relating In The IDE

Slide 29

Slide 29 text

Are You Ready To Go Back To Our Problems ?

Slide 30

Slide 30 text

Function Return

Slide 31

Slide 31 text

Function Return With TypeScript

Slide 32

Slide 32 text

Read File With Types

Slide 33

Slide 33 text

Third Party Authentication

Slide 34

Slide 34 text

Simple Function Call With Types

Slide 35

Slide 35 text

Now, Lets Talk About The Challenging Parts

Slide 36

Slide 36 text

Code Transpiler • Source to source compiler • Both TypeScript And Flow require using a code transpilar • Typescript - tsc - from Typescript To JavaScript • Flow - babel - traspiles .js files to .js files with no types • The code that you run is not the code that you write

Slide 37

Slide 37 text

Adding A Build Step • Can slow development • Every code change requires new build • Need to work with ‘npm watch’ to synchronise every code change • Can cause debugging problems in the IDE

Slide 38

Slide 38 text

Production - Using Source Maps • Adding a compilation step -> Source code is different from deployed code • Stack traces from production will appear on the deployed code • Without them - Impossible to debug • Need to use source maps to display stack traces from the original code • Crucial to analyse bugs on different environments (dev/ prod)

Slide 39

Slide 39 text

SourceMaps - No Unified Standard • TypeScript: • source-map-support package • Add source map support in tsconfig • Flow + Babel • babel-plugin-source-map-support • Make sure your cloud logging service supports source maps

Slide 40

Slide 40 text

Remember This ? It Is Still A Problem

Slide 41

Slide 41 text

Tamar Twena-Stern •Twitter: @SternTwena