SlideShare a Scribd company logo
Meetup Paris Typescript – 2016/11/24 - @Dashlane
Power Leveling your Typescript
From beginner to master in no time !
Who am I ?
Experienced developer
focusing on web for 5 years
@2016 getting things done at Dashlane
https://github.com/Offirmo
Welcome to the world of TypeScript
www.typescriptlang.org
So you want to become a typescript
master ?
Prove your worth.
“I want code that compile”
« As an enterprise web application developer, I don't like any scripting or
dynamic languages. I like code that compiles (...) TypeScript looks like it will
help with some of the problems like having (...) compiling (…) » (somewhere on the net)
No. You are asking for a linter. All languages have linters.
JS already has an excellent linter: ESLlint
TypeScript also has a linter: TSLint
They will catch typos and code smells. Use a linter. Now.
“Company BIG™ is using it”
● Created by Microsoft
● Embraced by Google: Angular 2
● Big contributions from Palantir (tslint, blueprint, …)
● Webpack is considering a rewrite in typescript
Following strong players’ lead may be a good move,
But could you make your own opinion ?
Yes or No ?
● Editor auto-completion
● Types = documentation
– Collaboration
– Faster usage
● Types = catching (some) errors
– Interfaces
– early
● Refactoring
●
...
● New language to learn
● NOT trivial
● More complicated stack
● Typings (find, fix, update…)
● Source maps
● Can’t use some JS tooling
● Bugs
● Lagging behind the standard
● Static typing sometimes refuses good
programs and has to be painfully hacked
● ...
Yes or No ?
● Editor auto-completion
● Types = documentation
– Collaboration
– Faster usage
● Types = catching (some) errors
– Interfaces
– early
● Refactoring
●
...
● New language to learn
● NOT trivial
● More complicated stack
● Typings (find, fix, update…)
● Source maps
● Can’t use some JS tooling
● Bugs
● Lagging behind the standard
● Static typing sometimes refuses good
programs and has to be painfully hacked
● ...
Short-term
Long-term
& important
Quick check
● Do you work in a team ? (3+)
● Do you have time right now ?
● Is it a long-term project or just a MVP ?
● Are you experienced ?
● Does your industry need the best ? (ex. Security)
● Do you like Typescript ?
Don’t !
● Not a substitute for Unit Tests
● Not a substitute for Code Reviews
● Not a way to go back to your OOP comfort zone
– Please learn about functional programming
 A Gentle Introduction to Functional JavaScript
Quest cleared !
“Understand why using typescript”
New Quest:
“Hello World in Typescript”
Do the tutorial: https://www.typescriptlang.org/docs/tutorial.html
Which Typescript ?
Go straight at TypeScript 2
(which is latest at this time 2016/11/24)
npm i –save-dev typescript
yarn add –dev typescript
Setup typescript: “tsconfig.json”
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html https://www.typescriptlang.org/docs/handbook/compiler-options.html
files to compile must be
declared
(more about this later)
{
"compileOnSave": false,
"compilerOptions": {
"allowJs": false,
"noEmitOnError": true,
"noImplicitAny": true,
(...)
"strictNullChecks": true,
"target": "ES6",
"module": "ES6",
"declaration": true,
"sourceMap": true,
"outDir": “temp/src.es6"
},
"include": [ "src/**/*.ts" ],
"exclude": [ "node_modules" ]
}
new project from scratch
or from existing code ?
choose the errors to catch !
or else your IDE may launch
transpilation !
output control
Quest cleared !
“Hello typescript”
Side-quest: “Know your transpiler”
Module
ES6
CommonJS
AMD
UMD
SystemJS
Target
ES3
ES5
ES6
You can pick whatever combo (Module + Target)
Execute typescript: node 6 example
Target: node v6
99% ES6
CommonJS
tsc
target = ES6
module = commonjsSource: Typescript
100% ES6, ?% ES7
ES6 modules
Execute typescript: node 4 example
tsc
target = ES6
module = ES6
Target: node v4
57% ES6
CommonJS
tsc
target = ES5
module = commonjs
Source: Typescript
100% ES6, ?% ES7
ES6 modules
Intermediate: ES6
100% ES6
ES6 modules
babel
babel-preset-es2015-
node4
FYI: an alternative stack ☟
New Quest:
“Get Things Done in Typescript”
Write types !
● boolean
● number
● string
● void
● Function
● null
● undefined
Combine them:
Interface Person {
name: string
age: number
friends: Person[]
}
https://www.typescriptlang.org/docs/handbook/basic-types.html
Nullable by default !
interface Foo {
name: string
age: number
}
interface Foo {
name: string
age: number | undefined
}
const x: Foo = {
name: undefined,
age: undefined
}
const x: Foo = {
name: undefined,
age: undefined
}
"StrictNullChecks": true (tsconfig.json)
enum vs. unions
const enum Directions {
Up,
Down,
Left,
Right
}
type Directions = 'up' | 'down' | 'left' | 'right'
https://www.typescriptlang.org/docs/handbook/enums.html
↓This writing is very convenient
Type vs. Interface
● Use interface for grouping
● Use type to define aliases:
type TimeoutMs = number
type AdventureModel =
JsonSchemaBasedModel<IAdventure>
Modules Detection
● If no exports, not a module = global variables
Argument destructuring
function displayPopup(
{onCancel, onConfirm}: {onCancel:()=>void, onConfirm:()=>void}
): void { …
interface Params {
onCancel: () => void
onConfirm: () => void
}
function displayPopup({onCancel, onConfirm}: Params): void { ...
https://www.barbarianmeetscoding.com/blog/2016/05/13/argument-destructuring-and-type-annotations-in-typescript/
Hashes and JSON
● Hashes can be typed:
interface Colors {[k: string]: Color }
● JSON is a declared type, can be extended:
interface JsonSchema extends JSON {
title: string
additionalProperties: boolean
[k: string]: any
}
overloading
● Not that simple. Better handled in code itself.
function hello(locutor: string, ...locutors: string[]): void {
locutors.unshift(locutor)
console.log(`Hello, ${locutors.join(', ')} !`)
}
hello('typescript world')
hello('Joe', 'Jack', 'William', 'Averell')
Type limitations
● Object.assign() => use as …
● .bind() => use ES6
Casting: as X, as any as X
● Casting from compatible type:
let x: any = { name: ‘Lothar’, age: 32 }
let user: Person = x as Person
● Force casting
let x: any = { name: ‘Lothar’ }
let user: Person = x as any as Person
Don’t overuse it ! Valid use case: mock data in tests
typeof
import * as axios from 'axios'
interface Dependencies { axios: typeof axios }
● smell
● hack
Can you feel your growth ?
Use a npm module
import { debounce } from lodash
Error !
Typescript CAN NOT consume
Javascript (directly)
Boss: “Dark Typings”
This boss can never be defeated, only repelled :-(
Need to use a “declaration file” (aka “typing”)
● A special kind of TypeScript file which “annotate” some JS:
npm I -D @types/lodash
● You NEED them. But may not exist, not be correct, not be up-to-date
● @types picked automatically by the compiler (since typescript 2)
● Sometimes needed: import * as _ from ‘lodash’
● Write your own: official doc, tutorial (More about this in next talk !)
http://stackoverflow.com/questions/38224232/how-to-consume-npm-modules-from-typescript
Boss chased away !
Choose your class to progress further…
Back-end (node) Front-end (browser) Universal (modules)
Back-end
● The simplest ;)
● npm I -D @types/node
● Execute directly: ts-node
– With a shebang
Front-end
Webpack
● typescript loader: ts-loader
React
● Complete class example here
● Special linting rules: tslint-react
● Typed CSS: typestyle
● limitations
Angular 2
● (see tutorials)
Execute typescript: browser
Target: browser
ES5
bundled
webpack
+ ts-loader
+ babel (for ES6
modules)
Source: Typescript
100% ES6, ?% ES7
ES6 modules
See also awesome-typescript-loader
Execute typescript: browser
tsc
target = ES6
module = ES6
+npm/cpx
Target: browser
ES5
bundled
Source: Typescript
100% ES6, ?% ES7
ES6 modules
Intermediate: ES6
100% ES6
ES6 modules
+ css, png...
webpack
+babel
FYI an alternate 2-steps stack which allows to use create-react-app without modification
(npm) npm-run + cpx
package.json/scripts
"dev1": "tsc --watch",
"dev2": "cpx 'src-ts/**/*.{json,css,png}' src",
"dev": "run-p dev1 dev2",
Universal (module)
● Generate several versions of
your code: node stable, ES6,
UMD
● Generate and expose the typings
● NEVER use default exports:
babel and typescript won’t work
well (see next slide)
● Beware of conflicting
environments
– Node vs. browser on SetTimeout
ES6 exports: no default !
● export default doesn’t play well with all platforms
● Use only named exports export { Foo, bar }
● Named exports are recommended in TypeScript regardless of this issue
● Interesting reads:
– http://stackoverflow.com/a/38671949/587407
– http://blog.jonasbandi.net/2016/10/myth-of-superset.html#comment-29295611
55
– http://www.jbrantly.com/es6-modules-with-typescript-and-webpack/
– https://github.com/webpack/webpack/issues/706
Real example of a universal TS npm module
● https://github.com/Offirmo/hello-world-npm
● All envs testable with
– https://github.com/Offirmo/cross-js-env-tests
Quest achieved
Next quests: “Advanced”
● Example of ts tyleguide: typescript coding guidelines
● TypeScript formatter: tsfmt
● Call the typescript compiler from a script:
– node-typescript-compiler
– Allows to dynamically change config
● Useful npm script: syntax watch:
"tsc:watch": "npm run tsc -- --watch",
● In special cases, you may have to tweak the –lib option:
--lib webworker
Final words: Superset ?
● Superset of JavaScript ?
● Comparison : C++ superset of C ?
● Superset of WELL-WRITTEN JavaScript ?
● http://blog.jonasbandi.net/2016/10/myth-of-superset.html#co
mment-2929561155
https://smellegantcode.wordpress.com/2015/07/11/is-typescript-really-a-superset-of-javascript-and-does-it-even-matter/
Thank you
Sources
● http://stackoverflow.com/questions/tagged/typescript?sort=frequent&pageSize=50
● https://www.reddit.com/r/typescript/comments/3fp1l2/why_bother_with_typescript/
● http://www.aaron-powell.com/posts/2013-01-07-thoughts-on-typescript.html
● https://smellegantcode.wordpress.com/2015/07/11/is-typescript-really-a-superset-of-javascript-and-does-it-even-matter/
● https://github.com/Offirmo-team/wiki/wiki/typescript
● I was wrong about typescript https://news.ycombinator.com/item?id=11841502
● http://www.jbrantly.com/es6-modules-with-typescript-and-webpack/
● http://www.jbrantly.com/typescript-and-webpack/
●
Resources
● http://mogmygear.com/index.php
● http://reznik.wikia.com/wiki/Axxa's_Wow_Logo_Creator
● http://achievementgen.com/wow/
● http://eu.battle.net/wow/fr/media/wallpapers/

More Related Content

What's hot (20)

PPTX
TypeScript Overview
Aniruddha Chakrabarti
 
PDF
Introduction to TypeScript by Winston Levi
Winston Levi
 
PDF
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
PPTX
Typescript in 30mins
Udaya Kumar
 
PPTX
Introducing type script
Remo Jansen
 
PDF
TypeScript Best Practices
felixbillon
 
PDF
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
PPT
Learning typescript
Alexandre Marreiros
 
PDF
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
PPTX
AngularConf2015
Alessandro Giorgetti
 
PPTX
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
PPTX
TypeScript Modules
Noam Kfir
 
PPTX
TypeScript 101
rachelterman
 
PDF
Introduction to Angular for .NET Developers
Laurent Duveau
 
PDF
Introduction to Angular for .NET Developers
Laurent Duveau
 
PPTX
Introduction about type script
Binh Quan Duc
 
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
Rutenis Turcinas
 
PPTX
Typescript 101 introduction
Bob German
 
PPTX
Typescript
Nikhil Thomas
 
TypeScript Overview
Aniruddha Chakrabarti
 
Introduction to TypeScript by Winston Levi
Winston Levi
 
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
Typescript in 30mins
Udaya Kumar
 
Introducing type script
Remo Jansen
 
TypeScript Best Practices
felixbillon
 
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Learning typescript
Alexandre Marreiros
 
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
AngularConf2015
Alessandro Giorgetti
 
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
TypeScript Modules
Noam Kfir
 
TypeScript 101
rachelterman
 
Introduction to Angular for .NET Developers
Laurent Duveau
 
Introduction to Angular for .NET Developers
Laurent Duveau
 
Introduction about type script
Binh Quan Duc
 
TypeScript - Silver Bullet for the Full-stack Developers
Rutenis Turcinas
 
Typescript 101 introduction
Bob German
 
Typescript
Nikhil Thomas
 

Viewers also liked (14)

PDF
Angular 2 - Typescript
Nathan Krasney
 
PDF
Typescript + Graphql = <3
felixbillon
 
PDF
TypeScript for Java Developers
Yakov Fain
 
PDF
TypeScript Introduction
Dmitry Sheiko
 
PDF
Александр Русаков - TypeScript 2 in action
MoscowJS
 
PPTX
Why TypeScript?
FITC
 
PPTX
002. Introducere in type script
Dmitrii Stoian
 
PDF
«Typescript: кому нужна строгая типизация?», Григорий Петров, MoscowJS 21
MoscowJS
 
PDF
TypeScript Seminar
Haim Michael
 
PPTX
Typescript tips & tricks
Ori Calvo
 
PPTX
TypeScript
GetDev.NET
 
PDF
TypeScript: Un lenguaje aburrido para programadores torpes y tristes
Micael Gallego
 
PDF
TypeScript: особенности разработки / Александр Майоров (Tutu.ru)
Ontico
 
PDF
TypeScriptで快適javascript
AfiruPain NaokiSoga
 
Angular 2 - Typescript
Nathan Krasney
 
Typescript + Graphql = <3
felixbillon
 
TypeScript for Java Developers
Yakov Fain
 
TypeScript Introduction
Dmitry Sheiko
 
Александр Русаков - TypeScript 2 in action
MoscowJS
 
Why TypeScript?
FITC
 
002. Introducere in type script
Dmitrii Stoian
 
«Typescript: кому нужна строгая типизация?», Григорий Петров, MoscowJS 21
MoscowJS
 
TypeScript Seminar
Haim Michael
 
Typescript tips & tricks
Ori Calvo
 
TypeScript
GetDev.NET
 
TypeScript: Un lenguaje aburrido para programadores torpes y tristes
Micael Gallego
 
TypeScript: особенности разработки / Александр Майоров (Tutu.ru)
Ontico
 
TypeScriptで快適javascript
AfiruPain NaokiSoga
 
Ad

Similar to Power Leveling your TypeScript (20)

PDF
Ruxmon.2013-08.-.CodeBro!
Christophe Alladoum
 
KEY
groovy & grails - lecture 6
Alexandre Masselot
 
PDF
Crystal internals (part 1)
Crystal Language
 
PDF
Crystal internals (part 1)
Ary Borenszweig
 
PDF
Crystal internals (part 1)
Ary Borenszweig
 
PDF
Ln monitoring repositories
snyff
 
PPTX
Java - A broad introduction
Birol Efe
 
PDF
TDD in Python With Pytest
Eddy Reyes
 
PDF
One language to rule them all type script
Gil Fink
 
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
PPTX
The advantage of developing with TypeScript
Corley S.r.l.
 
PDF
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
PDF
Not Your Fathers C - C Application Development In 2016
maiktoepfer
 
PDF
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
Igalia
 
PDF
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Codemotion
 
PDF
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
Diego Freniche Brito
 
PDF
Flow or Type - how to React to that?
Krešimir Antolić
 
PPTX
Rits Brown Bag - TypeScript
Right IT Services
 
PDF
Skiron - Experiments in CPU Design in D
Mithun Hunsur
 
PDF
Getting Started with the TypeScript Language
Gil Fink
 
Ruxmon.2013-08.-.CodeBro!
Christophe Alladoum
 
groovy & grails - lecture 6
Alexandre Masselot
 
Crystal internals (part 1)
Crystal Language
 
Crystal internals (part 1)
Ary Borenszweig
 
Crystal internals (part 1)
Ary Borenszweig
 
Ln monitoring repositories
snyff
 
Java - A broad introduction
Birol Efe
 
TDD in Python With Pytest
Eddy Reyes
 
One language to rule them all type script
Gil Fink
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
The advantage of developing with TypeScript
Corley S.r.l.
 
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
Not Your Fathers C - C Application Development In 2016
maiktoepfer
 
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
Igalia
 
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Codemotion
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
Diego Freniche Brito
 
Flow or Type - how to React to that?
Krešimir Antolić
 
Rits Brown Bag - TypeScript
Right IT Services
 
Skiron - Experiments in CPU Design in D
Mithun Hunsur
 
Getting Started with the TypeScript Language
Gil Fink
 
Ad

Recently uploaded (20)

PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Digital water marking system project report
Kamal Acharya
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Design Thinking basics for Engineers.pdf
CMR University
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Distribution reservoir and service storage pptx
dhanashree78
 

Power Leveling your TypeScript

  • 1. Meetup Paris Typescript – 2016/11/24 - @Dashlane Power Leveling your Typescript From beginner to master in no time !
  • 2. Who am I ? Experienced developer focusing on web for 5 years @2016 getting things done at Dashlane https://github.com/Offirmo
  • 3. Welcome to the world of TypeScript www.typescriptlang.org
  • 4. So you want to become a typescript master ? Prove your worth.
  • 5. “I want code that compile” « As an enterprise web application developer, I don't like any scripting or dynamic languages. I like code that compiles (...) TypeScript looks like it will help with some of the problems like having (...) compiling (…) » (somewhere on the net) No. You are asking for a linter. All languages have linters. JS already has an excellent linter: ESLlint TypeScript also has a linter: TSLint They will catch typos and code smells. Use a linter. Now.
  • 6. “Company BIG™ is using it” ● Created by Microsoft ● Embraced by Google: Angular 2 ● Big contributions from Palantir (tslint, blueprint, …) ● Webpack is considering a rewrite in typescript Following strong players’ lead may be a good move, But could you make your own opinion ?
  • 7. Yes or No ? ● Editor auto-completion ● Types = documentation – Collaboration – Faster usage ● Types = catching (some) errors – Interfaces – early ● Refactoring ● ... ● New language to learn ● NOT trivial ● More complicated stack ● Typings (find, fix, update…) ● Source maps ● Can’t use some JS tooling ● Bugs ● Lagging behind the standard ● Static typing sometimes refuses good programs and has to be painfully hacked ● ...
  • 8. Yes or No ? ● Editor auto-completion ● Types = documentation – Collaboration – Faster usage ● Types = catching (some) errors – Interfaces – early ● Refactoring ● ... ● New language to learn ● NOT trivial ● More complicated stack ● Typings (find, fix, update…) ● Source maps ● Can’t use some JS tooling ● Bugs ● Lagging behind the standard ● Static typing sometimes refuses good programs and has to be painfully hacked ● ... Short-term Long-term & important
  • 9. Quick check ● Do you work in a team ? (3+) ● Do you have time right now ? ● Is it a long-term project or just a MVP ? ● Are you experienced ? ● Does your industry need the best ? (ex. Security) ● Do you like Typescript ?
  • 10. Don’t ! ● Not a substitute for Unit Tests ● Not a substitute for Code Reviews ● Not a way to go back to your OOP comfort zone – Please learn about functional programming  A Gentle Introduction to Functional JavaScript
  • 11. Quest cleared ! “Understand why using typescript”
  • 12. New Quest: “Hello World in Typescript” Do the tutorial: https://www.typescriptlang.org/docs/tutorial.html
  • 13. Which Typescript ? Go straight at TypeScript 2 (which is latest at this time 2016/11/24) npm i –save-dev typescript yarn add –dev typescript
  • 14. Setup typescript: “tsconfig.json” https://www.typescriptlang.org/docs/handbook/tsconfig-json.html https://www.typescriptlang.org/docs/handbook/compiler-options.html files to compile must be declared (more about this later) { "compileOnSave": false, "compilerOptions": { "allowJs": false, "noEmitOnError": true, "noImplicitAny": true, (...) "strictNullChecks": true, "target": "ES6", "module": "ES6", "declaration": true, "sourceMap": true, "outDir": “temp/src.es6" }, "include": [ "src/**/*.ts" ], "exclude": [ "node_modules" ] } new project from scratch or from existing code ? choose the errors to catch ! or else your IDE may launch transpilation ! output control
  • 15. Quest cleared ! “Hello typescript”
  • 16. Side-quest: “Know your transpiler” Module ES6 CommonJS AMD UMD SystemJS Target ES3 ES5 ES6 You can pick whatever combo (Module + Target)
  • 17. Execute typescript: node 6 example Target: node v6 99% ES6 CommonJS tsc target = ES6 module = commonjsSource: Typescript 100% ES6, ?% ES7 ES6 modules
  • 18. Execute typescript: node 4 example tsc target = ES6 module = ES6 Target: node v4 57% ES6 CommonJS tsc target = ES5 module = commonjs Source: Typescript 100% ES6, ?% ES7 ES6 modules Intermediate: ES6 100% ES6 ES6 modules babel babel-preset-es2015- node4 FYI: an alternative stack ☟
  • 19. New Quest: “Get Things Done in Typescript”
  • 20. Write types ! ● boolean ● number ● string ● void ● Function ● null ● undefined Combine them: Interface Person { name: string age: number friends: Person[] } https://www.typescriptlang.org/docs/handbook/basic-types.html
  • 21. Nullable by default ! interface Foo { name: string age: number } interface Foo { name: string age: number | undefined } const x: Foo = { name: undefined, age: undefined } const x: Foo = { name: undefined, age: undefined } "StrictNullChecks": true (tsconfig.json)
  • 22. enum vs. unions const enum Directions { Up, Down, Left, Right } type Directions = 'up' | 'down' | 'left' | 'right' https://www.typescriptlang.org/docs/handbook/enums.html ↓This writing is very convenient
  • 23. Type vs. Interface ● Use interface for grouping ● Use type to define aliases: type TimeoutMs = number type AdventureModel = JsonSchemaBasedModel<IAdventure>
  • 24. Modules Detection ● If no exports, not a module = global variables
  • 25. Argument destructuring function displayPopup( {onCancel, onConfirm}: {onCancel:()=>void, onConfirm:()=>void} ): void { … interface Params { onCancel: () => void onConfirm: () => void } function displayPopup({onCancel, onConfirm}: Params): void { ... https://www.barbarianmeetscoding.com/blog/2016/05/13/argument-destructuring-and-type-annotations-in-typescript/
  • 26. Hashes and JSON ● Hashes can be typed: interface Colors {[k: string]: Color } ● JSON is a declared type, can be extended: interface JsonSchema extends JSON { title: string additionalProperties: boolean [k: string]: any }
  • 27. overloading ● Not that simple. Better handled in code itself. function hello(locutor: string, ...locutors: string[]): void { locutors.unshift(locutor) console.log(`Hello, ${locutors.join(', ')} !`) } hello('typescript world') hello('Joe', 'Jack', 'William', 'Averell')
  • 28. Type limitations ● Object.assign() => use as … ● .bind() => use ES6
  • 29. Casting: as X, as any as X ● Casting from compatible type: let x: any = { name: ‘Lothar’, age: 32 } let user: Person = x as Person ● Force casting let x: any = { name: ‘Lothar’ } let user: Person = x as any as Person Don’t overuse it ! Valid use case: mock data in tests
  • 30. typeof import * as axios from 'axios' interface Dependencies { axios: typeof axios } ● smell ● hack
  • 31. Can you feel your growth ?
  • 32. Use a npm module import { debounce } from lodash Error ! Typescript CAN NOT consume Javascript (directly)
  • 33. Boss: “Dark Typings” This boss can never be defeated, only repelled :-(
  • 34. Need to use a “declaration file” (aka “typing”) ● A special kind of TypeScript file which “annotate” some JS: npm I -D @types/lodash ● You NEED them. But may not exist, not be correct, not be up-to-date ● @types picked automatically by the compiler (since typescript 2) ● Sometimes needed: import * as _ from ‘lodash’ ● Write your own: official doc, tutorial (More about this in next talk !) http://stackoverflow.com/questions/38224232/how-to-consume-npm-modules-from-typescript
  • 36. Choose your class to progress further… Back-end (node) Front-end (browser) Universal (modules)
  • 37. Back-end ● The simplest ;) ● npm I -D @types/node ● Execute directly: ts-node – With a shebang
  • 38. Front-end Webpack ● typescript loader: ts-loader React ● Complete class example here ● Special linting rules: tslint-react ● Typed CSS: typestyle ● limitations Angular 2 ● (see tutorials)
  • 39. Execute typescript: browser Target: browser ES5 bundled webpack + ts-loader + babel (for ES6 modules) Source: Typescript 100% ES6, ?% ES7 ES6 modules See also awesome-typescript-loader
  • 40. Execute typescript: browser tsc target = ES6 module = ES6 +npm/cpx Target: browser ES5 bundled Source: Typescript 100% ES6, ?% ES7 ES6 modules Intermediate: ES6 100% ES6 ES6 modules + css, png... webpack +babel FYI an alternate 2-steps stack which allows to use create-react-app without modification
  • 41. (npm) npm-run + cpx package.json/scripts "dev1": "tsc --watch", "dev2": "cpx 'src-ts/**/*.{json,css,png}' src", "dev": "run-p dev1 dev2",
  • 42. Universal (module) ● Generate several versions of your code: node stable, ES6, UMD ● Generate and expose the typings ● NEVER use default exports: babel and typescript won’t work well (see next slide) ● Beware of conflicting environments – Node vs. browser on SetTimeout
  • 43. ES6 exports: no default ! ● export default doesn’t play well with all platforms ● Use only named exports export { Foo, bar } ● Named exports are recommended in TypeScript regardless of this issue ● Interesting reads: – http://stackoverflow.com/a/38671949/587407 – http://blog.jonasbandi.net/2016/10/myth-of-superset.html#comment-29295611 55 – http://www.jbrantly.com/es6-modules-with-typescript-and-webpack/ – https://github.com/webpack/webpack/issues/706
  • 44. Real example of a universal TS npm module ● https://github.com/Offirmo/hello-world-npm ● All envs testable with – https://github.com/Offirmo/cross-js-env-tests
  • 46. Next quests: “Advanced” ● Example of ts tyleguide: typescript coding guidelines ● TypeScript formatter: tsfmt ● Call the typescript compiler from a script: – node-typescript-compiler – Allows to dynamically change config ● Useful npm script: syntax watch: "tsc:watch": "npm run tsc -- --watch", ● In special cases, you may have to tweak the –lib option: --lib webworker
  • 47. Final words: Superset ? ● Superset of JavaScript ? ● Comparison : C++ superset of C ? ● Superset of WELL-WRITTEN JavaScript ? ● http://blog.jonasbandi.net/2016/10/myth-of-superset.html#co mment-2929561155 https://smellegantcode.wordpress.com/2015/07/11/is-typescript-really-a-superset-of-javascript-and-does-it-even-matter/
  • 49. Sources ● http://stackoverflow.com/questions/tagged/typescript?sort=frequent&pageSize=50 ● https://www.reddit.com/r/typescript/comments/3fp1l2/why_bother_with_typescript/ ● http://www.aaron-powell.com/posts/2013-01-07-thoughts-on-typescript.html ● https://smellegantcode.wordpress.com/2015/07/11/is-typescript-really-a-superset-of-javascript-and-does-it-even-matter/ ● https://github.com/Offirmo-team/wiki/wiki/typescript ● I was wrong about typescript https://news.ycombinator.com/item?id=11841502 ● http://www.jbrantly.com/es6-modules-with-typescript-and-webpack/ ● http://www.jbrantly.com/typescript-and-webpack/ ●
  • 50. Resources ● http://mogmygear.com/index.php ● http://reznik.wikia.com/wiki/Axxa's_Wow_Logo_Creator ● http://achievementgen.com/wow/ ● http://eu.battle.net/wow/fr/media/wallpapers/