SlideShare a Scribd company logo
Tech lead Co-orga Organisateur
@felix_billon
felixbillon
http://shakedatcode.com
Member
Félix Billon
• Introduction
• Architecture
• Transpilation
• Typing
• Migration JS → TS
• Conclusion
Sommaire
Introduction
Popularity
Github npm
Stackoverflow
Raise of TypeScript
TypeScript in open source world
Visual Studio Code Angular Ionic / Stencil RxJS
NativeScript TensorFlow.js NestJS Vue.js
In progress 👍
• Superset of Javascript.
• Made in Microsoft.
• Open source on GitHub.
• 2014 v1.0 -> today v3.2.
• Main features :
• Transpilation -> generates Javascript.
• Typing -> only useful during compilation.
TypeScript ?
• No official style guide !
• Maybe coding guidelines on TypeScript’s Github ?
Why this talk ?
“Don't blindly adhere to any old advice”
Be careful with best practices
Architecture
Architecture
• Files + options → tsc → core compiler → JavaScript files.
• Use options :
• Command line :
• Configuration file aka tsconfig.json :
tsc **/*.ts –-target=es5 -–sourcemap=true
CLI tsc
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"removeComments": true,
"sourceMap": true
},
"include": ["src/**/*"]
}
• Use tsconfig.js in preference to command line.
• Initialize it this way :
CLI tsc
tsc --init
Transpilation
• ECMAScript = standard for scripting languages.
• ECMAScript implementation : javascript, actionscript, …
Browser
JS EngineEcmaScript
implements
Edge : Chakra
Chrome : V8
Firefox : SpiderMonkey
…
ECMAScript
ES5
2009
• Strict mode
• Getters/Setters
• ….
ECMAScript : historical
ES5
ES6
2009 2015
• Classes
• Arrow function
• Promise
• Destructuring
• Constants
• Modules
• Template Literals
• Map/Set
• Iterators
• Generators
• Symbol type
• …
ECMAScript : historical
ES5
ES6
ES7
2009 2015 2016
• Array.prototype.includes
• Exponentation Operator
ECMAScript : historical
ES5
ES6
ES7 ES8
2009 2015 2016 2017
• String padding
• Object.value/Object.entries
• Async/await
• Improve trailing comma
• Shared memory and atomics
ECMAScript : historical
ES5
ES6
ES7 ES8
2009 2015 2016 2017
ES9
2018
• Asynchronous Iteration
• Rest/Spread properties
• Improve Regex
• Promise.prototype.finally
• Template Literal Revision
ECMAScript : historical
Implementation rate d’ES7+
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
var Greeter = /** @class */ (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
}());
var greeter = new Greeter("world");
file.ts TypeScript compiler file.js
Transpilation
• Compilation option :
--target ES3 (default), ES5, ES2015, ES2016, ES2017,ES2018 or ESNEXT
Transpilation : configuration
.ts .js Load byCompilation
.js
Exécution des polyfill
Transpilation ->
Polyfill ->
if(typeof Array.prototype.filter !== "function") {
Array.prototype.filter = function() {
// implementation goes here
};
}
contient
Transpilation vs Polyfill
.ts .js Load byCompilation
.js Load by
Polyfill’s code is execute then
application’s code is loaded
Transpilation ->
Polyfill ->
Transpilation vs Polyfill
• Adapt transpilation level to targeted browsers.
• TypeScript don’t transpile everythings, solution :
• TypeScript + polyfills library (core-js, es6-shim, …)
• TypeScript + Babel =
Transpilation
• Export ES2015
• Import ES2015
• Before : AMD, CommonJS, UMD, System, ES2015.
• Over time there will be only one : ES2015
export class Animal {
// …
}
import { Animal } from "./animal";
Module
• Compilation option :
--module none, commonjs, amd, system, umd, es2015, or ESNext
Module : configuration
• Use ES2015 -> transpile if needed.
• To prevent ugly import :
1. In tsconfig.json use aliases path :
2. Don’t forget to also configure this aliases into your bundler’s config file.
3. Result :
import { Animal } from "../../../../../../../core/animal";
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@myProject/utils/*": ["app/utils/*"],
"@myPorject/core/*": ["app/core/*"]
}
}
}
import { Animal } from “@myProject/core/animal";
Module
enum Color {
Red,
Blue,
Green
}
let foo: Color = Color.Red;
let bar: string = Color[Color.Red];
"use strict";
var Color;
(function (Color) {
Color[Color["Red"] = 0] = "Red";
Color[Color["Blue"] = 1] = "Blue";
Color[Color["Green"] = 2] = "Green";
})(Color || (Color = {}));
let foo = Color.Red;
let bar = Color[Color.Red];
color.ts TypeScript compiler color.js
Enum
const enum Color {
Red,
Blue,
Green
}
let foo: Color = Color.Red;
"use strict";
let foo = 0 /* Red */;
color.ts TypeScript compiler color.js
let bar: string = Color[Color.Red];
Constant Enum
• Use const enum as much as possible.
• Be careful with this option :
• If you acces Enum via index, thinks of Map/Set, Object, …
--preserveConstEnums
Enum
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
class PoliteGreeter extends Greeter {
//...
}
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Greeter = /** @class */ (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
}());
var PoliteGreeter = /** @class */ (function (_super) {
__extends(PoliteGreeter, _super);
function PoliteGreeter() {
return _super !== null && _super.apply(this, arguments) || this;
}
return PoliteGreeter;
}(Greeter));
file.ts TypeScript compiler file.js
TypeScript Helper
• Many helpers exists :
• Generate in each file where are needed -> increase bundle size !!!
function __assign(t: any, ...sources: any[]): any; // Helper de Object.Assign
function __spread(...args: any[]): any[]; // Helper de l'opérateur spread
//...
TypeScript Helper : the trap
• To prevent helpers to proliferate :
1. Install tslib :
2. Use the following compilation options :
3. Once done, TypeScript compiler only imports helpers from tslib
--importHelpers
--noEmitHelpers
npm install tslib
TypeScript Helper
Typing
Basic typing
• boolean, number, string, array, void, null, undefined, object, any et unknow.
let name: string;
let list: number[] = [1, 2, 3];
function fn(param: boolean): void {
// Do something
}
• Use any as less as possible !
• Prefer unknow to any : let myAny : any = "toto" ;
let myUnknown: unknown = "toto";
let foo: string = myAny;
let bar: string = myUnknown;
myAny.mehtod();
myUnknown.mehtod();
Basic typing
• Don’t have to type everythings, let TypeScript compiler inference do !
• Reminder : types are useful only during compilation not at runtime !
Basic typing
Classe and interface (1/2)
interface Ninja {
nbShuriken: number;
throwShuriken: () => void;
}
let leonardo: Ninja = new NinjaTurtle();
let donatelo: NinjaTutle = new NinjaTurtle();
class NinjaTurtle implements Ninja {
nbShuriken: number;
constructor() {
this.nbShuriken = 6;
}
throwShuriken(): void {
// Throw shuriken
}
}
Classe and interface (2/2)
interface Animal {
name: string;
say?: () => void;
}
interface Animal {
readonly name: string;
say?: () => void;
}
• Union :
• Intersection :
Union and intersection
class Ninja {
nbShuriken: number;
throwShuriken: () => void;
}
class Samurai {
nbKunai: number;
throwKunai: () => void;
}
assign<T, U>(target: T, source: U): T & U;
function throwAttack(human: Ninja | Samurai) {
if (human instanceof Ninja) {
human.throwShuriken();
} else {
human.throwKunai();
}
}
Type alias
class Ninja {
nbShuriken: number;
throwShuriken: () => void;
}
class Samurai {
nbKunai: number;
throwKunai: () => void;
}
type Fighter = Ninja | Samurai;
function throwAttack(human: Fighter) {
if (human instanceof Ninja) {
human.throwShuriken();
} else {
human.throwKunai();
}
}
• Which one use ?
• Need implementation -> Classe.
• Need union or intersection -> Alias.
• Otherwise -> Interface or Alias, make a choice and stick to it ☺
Classe vs Interface vs Alias
class Order {
id: number;
}
class User {
id: number;
name: string;
}
function processOrder(order: Order) {
// Do something
}
const order = new Order();
const user = new User();
processOrder(order);
processOrder(user);
Structural typings vs nominal typings
• “On TypeScript’s roadmap !” -> Investigation.
• One of many hack to force nominal typings :
class Order {
private __nominal: void;
id: number;
}
class User {
private __nominal: void;
id: number;
name: string;
}
function processOrder(order: Order) {
// Do something
}
const order = new Order();
const user = new User();
processOrder(order);
processOrder(user);
Do nominal typgins
• Compilation option :
• Master option that enable following sub-options :
--strict
--noImplicitAny
--noImplicitThis
--alwaysStrict
--strictNullChecks
--strictFunctionTypes
--strictPropertyInitialization
Enable stricter type checking (1/2)
• Enable immediately on new project, by default when use :
• Enable incrementally on existing project :
Enable stricter type checking (1/2)
{
"compilerOptions": {
"strict": true,
// "noImplicitAny": false,
"strictNullChecks": false,
"strictFunctionTypes": false,
"strictPropertyInitialization": false,
"noImplicitThis": false,
"alwaysStrict": false
}
}
tsc --init
👌
👌
✋
• TypeScript compiler use definition files for native JavaScript : lib.d.ts
Definition file
• Install :
• package.json :
Definition file
npm install -–save-dev @types/angular
{
"name": "angularjs-with-dts",
"version": "1.0.0",
"dependencies": {
"angular": "1.5.8"
},
"devDependencies": {
"@types/angular":"1.5.20"
}
}
• Alaway install .d.ts files in devDependencies.
• Specify composition of lib.d.ts file according to the native Javascript
features you use :
Definition file
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"es5",
"es2015.collection",
"es2015.iterable"
]
}
}
Migration JS -> TS
// @ts-check
JavaScript → TypeScript : solution 1
function add(numbers) {
return numbers
.reduce(function(previous, next) {
return previous + next;
});
}
var result = add([true, 2, "3"]);
console.log(result); // 33
// @ts-check
/**
* @param {number[]} numbers
*/
function add(numbers) {
return numbers
.reduce(function(previous, next) {
return previous + next;
});
}
var result = add([true, 2, "3"]);
console.log(result); // 33
--checkJs
• Incremental migration :
1. Create tsoncifg.config thanks to CLI tsc :
2. Set the following compilation option :
3. Adapt transpilation level.
4. Rename gradually file.js → file.ts
JavaScript → TypeScript : solution 2
--allowJs
tsc --init
• Prefer solution 2, if you can.
• TypeScript can transpile even if errors are decteded.
Migration JavaScript → TypeScript
Conclusion
Conslusion
• Essential to master TypeScript → Compilation options !
• Many subject not addressed :
• Options needed to use react, angular,…
• Mapped type and Conditional type
• …

More Related Content

What's hot

Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
Remo Jansen
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
akhilsreyas
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
Patrick John Pacaña
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
Aniruddha Chakrabarti
 
TypeScript
TypeScriptTypeScript
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
Lilia Sfaxi
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Samundra khatri
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?
Saulius Skeirys
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
Ats Uiboupin
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
ritika1
 
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid RahimianAPI Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
Vahid Rahimian
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
Iwan van der Kleijn
 
TypeScript
TypeScriptTypeScript
TypeScript
Saray Chak
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
André Pitombeira
 
Angular overview
Angular overviewAngular overview
Angular overview
Thanvilahari
 
React Hooks
React HooksReact Hooks
React Hooks
Joao Marins
 
React Context API
React Context APIReact Context API
React Context API
NodeXperts
 
React hooks
React hooksReact hooks
React hooks
Assaf Gannon
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
Nikolaus Graf
 

What's hot (20)

Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
TypeScript
TypeScriptTypeScript
TypeScript
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid RahimianAPI Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
TypeScript
TypeScriptTypeScript
TypeScript
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Angular overview
Angular overviewAngular overview
Angular overview
 
React Hooks
React HooksReact Hooks
React Hooks
 
React Context API
React Context APIReact Context API
React Context API
 
React hooks
React hooksReact hooks
React hooks
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 

Similar to TypeScript Best Practices

TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
Ralph Johnson
 
Type script
Type scriptType script
Type script
Zunair Shoes
 
Kotlin coroutines and spring framework
Kotlin coroutines and spring frameworkKotlin coroutines and spring framework
Kotlin coroutines and spring framework
Sunghyouk Bae
 
XAML/C# to HTML/JS
XAML/C# to HTML/JSXAML/C# to HTML/JS
XAML/C# to HTML/JS
Michael Haberman
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
Yakov Fain
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...
Arthur Puthin
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Hans Höchtl
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
Santosh Rajan
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
aleks-f
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
Oswald Campesato
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
Golang
GolangGolang
Golang
Felipe Mamud
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
Troy Miles
 
Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with Embind
Chad Austin
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
Christian Nagel
 

Similar to TypeScript Best Practices (20)

TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
 
Type script
Type scriptType script
Type script
 
Kotlin coroutines and spring framework
Kotlin coroutines and spring frameworkKotlin coroutines and spring framework
Kotlin coroutines and spring framework
 
XAML/C# to HTML/JS
XAML/C# to HTML/JSXAML/C# to HTML/JS
XAML/C# to HTML/JS
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
Apache Thrift
Apache ThriftApache Thrift
Apache Thrift
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...
 
Day 1
Day 1Day 1
Day 1
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Golang
GolangGolang
Golang
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
 
Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with Embind
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
 

More from felixbillon

typescript_cdktf.pptx
typescript_cdktf.pptxtypescript_cdktf.pptx
typescript_cdktf.pptx
felixbillon
 
Un problème 10 solutions : Azure Fighter
Un problème 10 solutions : Azure FighterUn problème 10 solutions : Azure Fighter
Un problème 10 solutions : Azure Fighter
felixbillon
 
Présentation et dernières nouveautés Microsoft Bot
Présentation et dernières nouveautés Microsoft BotPrésentation et dernières nouveautés Microsoft Bot
Présentation et dernières nouveautés Microsoft Bot
felixbillon
 
Global Azure Bootcamp 2018 - Microsoft Bot
Global Azure Bootcamp 2018 - Microsoft BotGlobal Azure Bootcamp 2018 - Microsoft Bot
Global Azure Bootcamp 2018 - Microsoft Bot
felixbillon
 
Présentation Google Cloud Vision API
Présentation Google Cloud Vision APIPrésentation Google Cloud Vision API
Présentation Google Cloud Vision API
felixbillon
 
Typescript + Graphql = <3
Typescript + Graphql = <3Typescript + Graphql = <3
Typescript + Graphql = <3
felixbillon
 
Service cognitifs : vue d'ensemble
Service cognitifs : vue d'ensembleService cognitifs : vue d'ensemble
Service cognitifs : vue d'ensemble
felixbillon
 
Introduction TypeScript
Introduction TypeScriptIntroduction TypeScript
Introduction TypeScript
felixbillon
 

More from felixbillon (8)

typescript_cdktf.pptx
typescript_cdktf.pptxtypescript_cdktf.pptx
typescript_cdktf.pptx
 
Un problème 10 solutions : Azure Fighter
Un problème 10 solutions : Azure FighterUn problème 10 solutions : Azure Fighter
Un problème 10 solutions : Azure Fighter
 
Présentation et dernières nouveautés Microsoft Bot
Présentation et dernières nouveautés Microsoft BotPrésentation et dernières nouveautés Microsoft Bot
Présentation et dernières nouveautés Microsoft Bot
 
Global Azure Bootcamp 2018 - Microsoft Bot
Global Azure Bootcamp 2018 - Microsoft BotGlobal Azure Bootcamp 2018 - Microsoft Bot
Global Azure Bootcamp 2018 - Microsoft Bot
 
Présentation Google Cloud Vision API
Présentation Google Cloud Vision APIPrésentation Google Cloud Vision API
Présentation Google Cloud Vision API
 
Typescript + Graphql = <3
Typescript + Graphql = <3Typescript + Graphql = <3
Typescript + Graphql = <3
 
Service cognitifs : vue d'ensemble
Service cognitifs : vue d'ensembleService cognitifs : vue d'ensemble
Service cognitifs : vue d'ensemble
 
Introduction TypeScript
Introduction TypeScriptIntroduction TypeScript
Introduction TypeScript
 

Recently uploaded

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 

Recently uploaded (20)

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 

TypeScript Best Practices

  • 1.
  • 2. Tech lead Co-orga Organisateur @felix_billon felixbillon http://shakedatcode.com Member Félix Billon
  • 3. • Introduction • Architecture • Transpilation • Typing • Migration JS → TS • Conclusion Sommaire
  • 6. TypeScript in open source world Visual Studio Code Angular Ionic / Stencil RxJS NativeScript TensorFlow.js NestJS Vue.js In progress 👍
  • 7. • Superset of Javascript. • Made in Microsoft. • Open source on GitHub. • 2014 v1.0 -> today v3.2. • Main features : • Transpilation -> generates Javascript. • Typing -> only useful during compilation. TypeScript ?
  • 8. • No official style guide ! • Maybe coding guidelines on TypeScript’s Github ? Why this talk ?
  • 9. “Don't blindly adhere to any old advice” Be careful with best practices
  • 12. • Files + options → tsc → core compiler → JavaScript files. • Use options : • Command line : • Configuration file aka tsconfig.json : tsc **/*.ts –-target=es5 -–sourcemap=true CLI tsc { "compilerOptions": { "target": "es5", "module": "es2015", "removeComments": true, "sourceMap": true }, "include": ["src/**/*"] }
  • 13. • Use tsconfig.js in preference to command line. • Initialize it this way : CLI tsc tsc --init
  • 15. • ECMAScript = standard for scripting languages. • ECMAScript implementation : javascript, actionscript, … Browser JS EngineEcmaScript implements Edge : Chakra Chrome : V8 Firefox : SpiderMonkey … ECMAScript
  • 16. ES5 2009 • Strict mode • Getters/Setters • …. ECMAScript : historical
  • 17. ES5 ES6 2009 2015 • Classes • Arrow function • Promise • Destructuring • Constants • Modules • Template Literals • Map/Set • Iterators • Generators • Symbol type • … ECMAScript : historical
  • 18. ES5 ES6 ES7 2009 2015 2016 • Array.prototype.includes • Exponentation Operator ECMAScript : historical
  • 19. ES5 ES6 ES7 ES8 2009 2015 2016 2017 • String padding • Object.value/Object.entries • Async/await • Improve trailing comma • Shared memory and atomics ECMAScript : historical
  • 20. ES5 ES6 ES7 ES8 2009 2015 2016 2017 ES9 2018 • Asynchronous Iteration • Rest/Spread properties • Improve Regex • Promise.prototype.finally • Template Literal Revision ECMAScript : historical
  • 22. class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; } } let greeter = new Greeter("world"); var Greeter = /** @class */ (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return "Hello, " + this.greeting; }; return Greeter; }()); var greeter = new Greeter("world"); file.ts TypeScript compiler file.js Transpilation
  • 23. • Compilation option : --target ES3 (default), ES5, ES2015, ES2016, ES2017,ES2018 or ESNEXT Transpilation : configuration
  • 24. .ts .js Load byCompilation .js Exécution des polyfill Transpilation -> Polyfill -> if(typeof Array.prototype.filter !== "function") { Array.prototype.filter = function() { // implementation goes here }; } contient Transpilation vs Polyfill
  • 25. .ts .js Load byCompilation .js Load by Polyfill’s code is execute then application’s code is loaded Transpilation -> Polyfill -> Transpilation vs Polyfill
  • 26. • Adapt transpilation level to targeted browsers. • TypeScript don’t transpile everythings, solution : • TypeScript + polyfills library (core-js, es6-shim, …) • TypeScript + Babel = Transpilation
  • 27. • Export ES2015 • Import ES2015 • Before : AMD, CommonJS, UMD, System, ES2015. • Over time there will be only one : ES2015 export class Animal { // … } import { Animal } from "./animal"; Module
  • 28. • Compilation option : --module none, commonjs, amd, system, umd, es2015, or ESNext Module : configuration
  • 29. • Use ES2015 -> transpile if needed. • To prevent ugly import : 1. In tsconfig.json use aliases path : 2. Don’t forget to also configure this aliases into your bundler’s config file. 3. Result : import { Animal } from "../../../../../../../core/animal"; { "compilerOptions": { "baseUrl": "./src", "paths": { "@myProject/utils/*": ["app/utils/*"], "@myPorject/core/*": ["app/core/*"] } } } import { Animal } from “@myProject/core/animal"; Module
  • 30. enum Color { Red, Blue, Green } let foo: Color = Color.Red; let bar: string = Color[Color.Red]; "use strict"; var Color; (function (Color) { Color[Color["Red"] = 0] = "Red"; Color[Color["Blue"] = 1] = "Blue"; Color[Color["Green"] = 2] = "Green"; })(Color || (Color = {})); let foo = Color.Red; let bar = Color[Color.Red]; color.ts TypeScript compiler color.js Enum
  • 31. const enum Color { Red, Blue, Green } let foo: Color = Color.Red; "use strict"; let foo = 0 /* Red */; color.ts TypeScript compiler color.js let bar: string = Color[Color.Red]; Constant Enum
  • 32. • Use const enum as much as possible. • Be careful with this option : • If you acces Enum via index, thinks of Map/Set, Object, … --preserveConstEnums Enum
  • 33. class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; } } class PoliteGreeter extends Greeter { //... } var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var Greeter = /** @class */ (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return "Hello, " + this.greeting; }; return Greeter; }()); var PoliteGreeter = /** @class */ (function (_super) { __extends(PoliteGreeter, _super); function PoliteGreeter() { return _super !== null && _super.apply(this, arguments) || this; } return PoliteGreeter; }(Greeter)); file.ts TypeScript compiler file.js TypeScript Helper
  • 34. • Many helpers exists : • Generate in each file where are needed -> increase bundle size !!! function __assign(t: any, ...sources: any[]): any; // Helper de Object.Assign function __spread(...args: any[]): any[]; // Helper de l'opérateur spread //... TypeScript Helper : the trap
  • 35. • To prevent helpers to proliferate : 1. Install tslib : 2. Use the following compilation options : 3. Once done, TypeScript compiler only imports helpers from tslib --importHelpers --noEmitHelpers npm install tslib TypeScript Helper
  • 37. Basic typing • boolean, number, string, array, void, null, undefined, object, any et unknow. let name: string; let list: number[] = [1, 2, 3]; function fn(param: boolean): void { // Do something }
  • 38. • Use any as less as possible ! • Prefer unknow to any : let myAny : any = "toto" ; let myUnknown: unknown = "toto"; let foo: string = myAny; let bar: string = myUnknown; myAny.mehtod(); myUnknown.mehtod(); Basic typing
  • 39. • Don’t have to type everythings, let TypeScript compiler inference do ! • Reminder : types are useful only during compilation not at runtime ! Basic typing
  • 40. Classe and interface (1/2) interface Ninja { nbShuriken: number; throwShuriken: () => void; } let leonardo: Ninja = new NinjaTurtle(); let donatelo: NinjaTutle = new NinjaTurtle(); class NinjaTurtle implements Ninja { nbShuriken: number; constructor() { this.nbShuriken = 6; } throwShuriken(): void { // Throw shuriken } }
  • 41. Classe and interface (2/2) interface Animal { name: string; say?: () => void; } interface Animal { readonly name: string; say?: () => void; }
  • 42. • Union : • Intersection : Union and intersection class Ninja { nbShuriken: number; throwShuriken: () => void; } class Samurai { nbKunai: number; throwKunai: () => void; } assign<T, U>(target: T, source: U): T & U; function throwAttack(human: Ninja | Samurai) { if (human instanceof Ninja) { human.throwShuriken(); } else { human.throwKunai(); } }
  • 43. Type alias class Ninja { nbShuriken: number; throwShuriken: () => void; } class Samurai { nbKunai: number; throwKunai: () => void; } type Fighter = Ninja | Samurai; function throwAttack(human: Fighter) { if (human instanceof Ninja) { human.throwShuriken(); } else { human.throwKunai(); } }
  • 44. • Which one use ? • Need implementation -> Classe. • Need union or intersection -> Alias. • Otherwise -> Interface or Alias, make a choice and stick to it ☺ Classe vs Interface vs Alias
  • 45. class Order { id: number; } class User { id: number; name: string; } function processOrder(order: Order) { // Do something } const order = new Order(); const user = new User(); processOrder(order); processOrder(user); Structural typings vs nominal typings
  • 46. • “On TypeScript’s roadmap !” -> Investigation. • One of many hack to force nominal typings : class Order { private __nominal: void; id: number; } class User { private __nominal: void; id: number; name: string; } function processOrder(order: Order) { // Do something } const order = new Order(); const user = new User(); processOrder(order); processOrder(user); Do nominal typgins
  • 47. • Compilation option : • Master option that enable following sub-options : --strict --noImplicitAny --noImplicitThis --alwaysStrict --strictNullChecks --strictFunctionTypes --strictPropertyInitialization Enable stricter type checking (1/2)
  • 48. • Enable immediately on new project, by default when use : • Enable incrementally on existing project : Enable stricter type checking (1/2) { "compilerOptions": { "strict": true, // "noImplicitAny": false, "strictNullChecks": false, "strictFunctionTypes": false, "strictPropertyInitialization": false, "noImplicitThis": false, "alwaysStrict": false } } tsc --init
  • 49. 👌 👌 ✋ • TypeScript compiler use definition files for native JavaScript : lib.d.ts Definition file
  • 50. • Install : • package.json : Definition file npm install -–save-dev @types/angular { "name": "angularjs-with-dts", "version": "1.0.0", "dependencies": { "angular": "1.5.8" }, "devDependencies": { "@types/angular":"1.5.20" } }
  • 51. • Alaway install .d.ts files in devDependencies. • Specify composition of lib.d.ts file according to the native Javascript features you use : Definition file { "compilerOptions": { "target": "es5", "lib": [ "dom", "es5", "es2015.collection", "es2015.iterable" ] } }
  • 53. // @ts-check JavaScript → TypeScript : solution 1 function add(numbers) { return numbers .reduce(function(previous, next) { return previous + next; }); } var result = add([true, 2, "3"]); console.log(result); // 33 // @ts-check /** * @param {number[]} numbers */ function add(numbers) { return numbers .reduce(function(previous, next) { return previous + next; }); } var result = add([true, 2, "3"]); console.log(result); // 33 --checkJs
  • 54. • Incremental migration : 1. Create tsoncifg.config thanks to CLI tsc : 2. Set the following compilation option : 3. Adapt transpilation level. 4. Rename gradually file.js → file.ts JavaScript → TypeScript : solution 2 --allowJs tsc --init
  • 55. • Prefer solution 2, if you can. • TypeScript can transpile even if errors are decteded. Migration JavaScript → TypeScript
  • 57. Conslusion • Essential to master TypeScript → Compilation options ! • Many subject not addressed : • Options needed to use react, angular,… • Mapped type and Conditional type • …