Building applications with Node & TypeScript

Building applications with Node & TypeScript

An introduction to TypeScript and how to combine it with NodeJs to create awesome applications.

1bbe893cbb494c230fe608b3783ac7bd?s=128

Marco Talento

April 16, 2016
Tweet

Transcript

  1. Building applications with Node & TypeScript @Talento90 marcotalento90@gmail.com Marco Talento

  2. About me • R&D 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 2
  3. Agenda • Introduction • TS definitions • Tools & IDE’s

    • Project Structure • Demo • Conclusion 3
  4. What is 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... 4
  5. Who is using TypeScript? 5

  6. Why TypeScript? • Easy to learn • Types prevents bugs

    • Makes code more readable • Open Source • Created & maintained by Microsoft 6
  7. Types Prevent Bugs 7

  8. How to “TypeScript”? 1. Install TypeScript: 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 8
  9. Compilation result 9

  10. Compiler options Use tsconfig.json to specify the compiler options. tsconfig.json

    - http://www.typescriptlang.org/docs/handbook/tsconfig.json.html 10
  11. TypeScript Definitions 11

  12. What is a TS definition? • 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: /// <reference path="../../../typings.d.ts" /> 12
  13. Example 13

  14. MongoDb Definition 14

  15. TypeScript Definition Manager Typings is a tool to manage and

    install TypeScript definitions. It uses typings.json to handle dependencies. Github: https://github.com/typings/typings How to use it: • Install typings: npm install typings --global • Search for typings: typings search hapi • Install Typings: typings install hapi --ambient --save 15
  16. typings.json 16

  17. Definition doesn’t exist? 17

  18. No problem! We have 3 different solutions! 18

  19. First Solution (Don’t Care) 19 Ignore the TS errors, the

    generated code will work!
  20. Second Solution (Lazy) 20 Create a definition to shut up

    the compiler.
  21. Third Solution (Perfect) 21 Create and contribute to the community

    http://definitelytyped.org/guides/contributing.html
  22. Tools & IDE’s TSLint - A Linter for the TypeScript

    language. Typedoc - A documentation generator for TypeScript projects. IDE Support: • VS Code (https://code.visualstudio.com/) • Sublime (https://github.com/Microsoft/TypeScript-Sublime-Plugin) • Atom (https://atom.io/packages/atom-typescript) • Vim (https://github.com/Microsoft/TypeScript/wiki/TypeScript-Editor- Support#vim) 22
  23. Project Structure 23

  24. Project Structure src - TS source files. test - all

    application tests must be inside this folder. build - contains all the generated JS files respecting the same structure. 24
  25. Project Structure • gulpfile.js - define your tasks (compile, test,

    etc…) • tsconfig.json - TypeScript compiler options. • tslint.json - TypeScript linter rules • typings.json - Typings Definition Manager • typings.d.ts - Main file that aggregates all definitions. 25
  26. DEMO (Putting Everything Together) 26 Code: https://github.com/Talento90/typescript-node

  27. Conclusion & Thoughts 27 • Easier to write scalable &

    reliable code • Improve developers productivity • Can be used everywhere • Better documentation • The community is growing faster
  28. References 28 • http://www.typescriptlang.org/ • http://johnpapa.net/es5-es2015-typescript/ • https://basarat.gitbooks.io/typescript/content/docs/getting-started.html • https://medium.com/ants-blog/beat-the-typescript-monster-d3653458a1fe

    • http://staltz.com/all-js-libraries-should-be-authored-in-typescript.html
  29. Thanks! 29

  30. Q&A 30