Lors de cette présentation, nous apprendrons à créer des applications Web plus rapidement et avec moins d'erreurs en utilisant un langage de programmation puissant et amusant.
Agenda
- Installer TypeScript et configurer un nouveau projet.
- Tirer avantage des types de données.
- Développer en Objets avec TypeScript
- Ecrire de meilleures fonctions
- Retrouver vos données avec LINQ
- Programmer de manière asynchrone
- Bonnes pratiques
- Avantages et inconvénients des projets TypeScript
- Conclusion et Discussion
5. JavaScript
Problèmes du langage
• Langage interprété
• Absence de modules
• Typage faible
• Conversions automatiques
• Portée (this)
• Manque de types statiques
6. Programming language
Superset of JavaScript
Transpiled into JavaScript
Open Source
Developped by Microsoft, supported by Google
lang.org
7. «Microsoft's TypeScript may be the best of the
many Javascript front ends. It seems to generate
the most attractive code.»
https://insights.stackoverflow.com/survey/2018
16. Basic Types
Boolean - Number - String - Array - Enum
var name: string = "Denis"
var age: number = 21
var birthdate: Date;
var name = "Denis";
var age = 21;
var birthdate;
birthdate.
17. Declarations with let and const
console.log(someString);
var someString = "hello World";
let someString = "hello World";
console.log(someString);
const someString = "hello World";
someString = "a new World";
18. Type Annotations and Type Inference
let x: string = "Hello World";
x = 42;
let x = "Hello World";
x = 42;
let name: string = "Voituron";
let message = `My name is ${name}`;
19. Union Types, Null, Undefined
{
"compilerOptions": {
"strictNullChecks": true,
...
let x: string;
x = null;
x = undefined;
let y: string | null;
y = null;
y = undefined;
let z: string | number | null;
z = null;
z = "Hello";
z = 123;
20. Type Assertions
let value: any = 5;
let fixedString: string = (<number>value).toFixed(4);
console.log(fixedString) // 5.0000
let value: any = 5;
let fixedString: string = (value as number).toFixed(4);
console.log(fixedString) // 5.0000
23. Method
function getSurface(width: number): number {
return width * width;
}
function getSurface(width): number {
return width * width;
}
[ts] Parameter 'width' implicitly
has an 'any' type.
24. Overloaded Methods
// Signatures
function getSurface(width: number): number;
function getSurface(width: number, height: number): number;
// Main method
function getSurface(width: number, height?: number): number {
if (height == null)
return width * width;
else
return width * height;
}
26. Anonymous methods
let square = (x: number) => x * x;
square(2); // 4
let add = (a: number, b: number) => a + b;
add(2, 3); // 5
let add = (a: number, b: number) => { return a + b };
27. Static Members
class WebDeveloper extends Developer {
static jobDescription = "Build cool things";
static logFavoriteProtocol() {
console.log("HTTPS, of course");
}
writeTypeScript(): void {
console.log(WebDeveloper.jobDescription);
}
}
28. Generic
"lib": [ "es6" ]
let list = new Set<number>([0, 1]);
list.add(2)
let dico = new Map<string, number>([["k1", 0], ["k2", 1]]);
dico.set("k1", 2);
let array = new Array<string>();
array.push("Hello");
29. Generic
class Queue {
private data = [];
push = (item) => this.data.push(item);
pop = () => this.data.shift();
}
const queue = new Queue();
queue.push(0);
queue.push("1"); // RUNTIME ERROR
"lib": [ "es6" ]
30. Generic
class QueueNumber {
private data = [];
push = (item: number) => this.data.push(item);
pop = (): number => this.data.shift();
}
const queue = new QueueNumber();
queue.push(0);
queue.push("1"); // COMPILATION ERROR
class Queue<T> {
private data = [];
push = (item: T) => this.data.push(item);
pop = (): T => this.data.shift();
}
const queue = new Queue<number>();
queue.push(0);
queue.push("1"); // COMPILATION ERROR
31. Interfaces vs Classes
Classes
Define a new type
Properties (with implementation)
Methods (with implémentation)
Can be instantiated
Interfaces
Define a new type
Properties (signatures)
Methods (signatures)
Cannot be instantiated
40. Exporting a declaration
export interface Person { }
class Manager { } // Not accessible outside the module
import { Person } from "./Person";
let manager: Person
41. Exporting a declaration
export interface Person { }
class Manager { } // Not accessible outside the module
import * as People from "./Person";
let manager: People.Person
54. Do's and Don'ts
• General Types
• Callback Types
• Optional Parameters in Callbacks
• Overloads and Callbacks
• Function Overloads
https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html
55. Programming language
Superset of JavaScript
Transpiled into JavaScript
Open Source
Developped by Microsoft, supported by Google
lang.org