Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How To Combine Types In Your Node.js Server

How To Combine Types In Your Node.js Server

Tamar Twena-Stern

May 14, 2019
Tweet

More Decks by Tamar Twena-Stern

Other Decks in Technology

Transcript

  1. Tamar Twena-Stern • Software Engineer - manager and architect •

    Architect @PaloAltoNetworks • Was a CTO of my own startup • Passionate about Node.js ! • Twitter: @SternTwena
  2. Tamar Twena-Stern • On Maternity Leave • Have 3 kids

    • Loves to play my violin • Javascript Israel community leader
  3. 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.
  4. JavaScript - Dynamically Typed language • No typing declarations •

    Runtime error checking • No types information • No return statement
  5. 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
  6. 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
  7. Transpiles To JavaScript Deploying and running JavaScript files TypeScript compiler

    create JavaScript files in build folder TypeScript source files
  8. 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
  9. Ts-node • An executable that we are running • Registers

    the TypeScript compiler for the relevant TypeScript file extensions. • Transpiling relevant extensions on the fly
  10. Flow • Open source package • developed by Facebook •

    Static type checker for JavaScript • Works with babel compiler • JavaScript files with flow annotations
  11. Flow Compiles With Babel Deploying and running JavaScript files Babel

    compiler create JavaScript files in build folder JavaScript source files with flow annotation
  12. Basic Usage • Annotate your code with types • Catch

    errors during “compile” time • No types = plane JavaScript.
  13. 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
  14. 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
  15. 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
  16. 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)
  17. 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