Building applications
with
Node & TypeScript
@Talento90 [email protected]
Marco Talento
Slide 2
Slide 2 text
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
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
Slide 5
Slide 5 text
Who is using TypeScript?
5
Slide 6
Slide 6 text
Why TypeScript?
●
Easy to learn
●
Types prevents bugs
●
Makes code more readable
●
Open Source
●
Created & maintained by Microsoft
6
Slide 7
Slide 7 text
Types Prevent Bugs
7
Slide 8
Slide 8 text
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
Slide 9
Slide 9 text
Compilation result
9
Slide 10
Slide 10 text
Compiler options
Use tsconfig.json to specify the compiler options.
tsconfig.json - http://www.typescriptlang.org/docs/handbook/tsconfig.json.html
10
Slide 11
Slide 11 text
TypeScript Definitions
11
Slide 12
Slide 12 text
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: ///
12
Slide 13
Slide 13 text
Example
13
Slide 14
Slide 14 text
MongoDb Definition
14
Slide 15
Slide 15 text
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
Slide 16
Slide 16 text
typings.json
16
Slide 17
Slide 17 text
Definition doesn’t exist?
17
Slide 18
Slide 18 text
No problem! We have 3 different solutions!
18
Slide 19
Slide 19 text
First Solution (Don’t Care)
19
Ignore the TS errors, the generated code will work!
Slide 20
Slide 20 text
Second Solution (Lazy)
20
Create a definition to shut up the compiler.
Slide 21
Slide 21 text
Third Solution (Perfect)
21
Create and contribute to the community
http://definitelytyped.org/guides/contributing.html
Slide 22
Slide 22 text
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
Slide 23
Slide 23 text
Project Structure
23
Slide 24
Slide 24 text
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
Slide 25
Slide 25 text
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
Conclusion & Thoughts
27
●
Easier to write scalable & reliable code
●
Improve developers productivity
●
Can be used everywhere
●
Better documentation
●
The community is growing faster