mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-11 17:24:29 +02:00
Update typescript-fr.html.markdown
Add classe block
This commit is contained in:
@@ -54,73 +54,70 @@ var f4 = (i: number) => { return i * i; }
|
|||||||
// Retourne un type inféré, ici le mot clé `return` n'est pas nécessaire
|
// Retourne un type inféré, ici le mot clé `return` n'est pas nécessaire
|
||||||
var f5 = (i: number) => i * i;
|
var f5 = (i: number) => i * i;
|
||||||
|
|
||||||
// Interfaces are structural, anything that has the properties is compliant with
|
// Les interfaces sont structurés, tout ce qui a les propriétés est compatible avec
|
||||||
// the interface
|
// l'interface
|
||||||
interface Person {
|
interface Person {
|
||||||
name: string;
|
name: string;
|
||||||
// Optional properties, marked with a "?"
|
// Les propriétés optionnelles sont identifiées avec un "?"
|
||||||
age?: number;
|
age?: number;
|
||||||
// And of course functions
|
// Et bien sûr, les fonctions
|
||||||
move(): void;
|
move(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Object that implements the "Person" interface
|
// Un objet implémentant l'interface "Person" peut être traité comme
|
||||||
// Can be treated as a Person since it has the name and move properties
|
// une Person car il a les propriétés "name" et "move"
|
||||||
var p: Person = { name: "Bobby", move: () => {} };
|
var p: Person = { name: "Bobby", move: () => {} };
|
||||||
// Objects that have the optional property:
|
// Des objets implémentants la propriété optionnelle :
|
||||||
var validPerson: Person = { name: "Bobby", age: 42, move: () => {} };
|
var validPerson: Person = { name: "Bobby", age: 42, move: () => {} }; // valide car "age" est un nombre
|
||||||
// Is not a person because age is not a number
|
var invalidPerson: Person = { name: "Bobby", age: true }; // invalide car "age" n'est pas un nombre
|
||||||
var invalidPerson: Person = { name: "Bobby", age: true };
|
|
||||||
|
|
||||||
// Interfaces can also describe a function type
|
// Les interfaces peuvent aussi décrire un type de fonction
|
||||||
interface SearchFunc {
|
interface SearchFunc {
|
||||||
(source: string, subString: string): boolean;
|
(source: string, subString: string): boolean;
|
||||||
}
|
}
|
||||||
// Only the parameters' types are important, names are not important.
|
// Seul les types des paramètres sont importants, les noms ne le sont pas
|
||||||
var mySearch: SearchFunc;
|
var mySearch: SearchFunc;
|
||||||
mySearch = function(src: string, sub: string) {
|
mySearch = function(src: string, sub: string) {
|
||||||
return src.search(sub) != -1;
|
return src.search(sub) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Classes - members are public by default
|
// Les membres des classes sont publiques par défaut
|
||||||
class Point {
|
class Point {
|
||||||
// Properties
|
// Propriétés
|
||||||
x: number;
|
x: number;
|
||||||
|
|
||||||
// Constructor - the public/private keywords in this context will generate
|
// Constructeur - Les mots clés "public" et "private" dans ce contexte génèrent
|
||||||
// the boiler plate code for the property and the initialization in the
|
// le code de la propriété et son initialisation dans le constructeur.
|
||||||
// constructor.
|
// Dans cet exemple, "y" sera défini de la même façon que "x", mais avec moins de code
|
||||||
// In this example, "y" will be defined just like "x" is, but with less code
|
// Les valeurs par défaut sont supportées
|
||||||
// Default values are also supported
|
|
||||||
|
|
||||||
constructor(x: number, public y: number = 0) {
|
constructor(x: number, public y: number = 0) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions
|
// Fonctions
|
||||||
dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }
|
dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }
|
||||||
|
|
||||||
// Static members
|
// Membres statiques
|
||||||
static origin = new Point(0, 0);
|
static origin = new Point(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var p1 = new Point(10 ,20);
|
var p1 = new Point(10 ,20);
|
||||||
var p2 = new Point(25); //y will be 0
|
var p2 = new Point(25); //y will be 0
|
||||||
|
|
||||||
// Inheritance
|
// Héritage
|
||||||
class Point3D extends Point {
|
class Point3D extends Point {
|
||||||
constructor(x: number, y: number, public z: number = 0) {
|
constructor(x: number, y: number, public z: number = 0) {
|
||||||
super(x, y); // Explicit call to the super class constructor is mandatory
|
super(x, y); // Un appel explicite au constructeur de la super classe est obligatoire.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite
|
// Redéfinition
|
||||||
dist() {
|
dist() {
|
||||||
var d = super.dist();
|
var d = super.dist();
|
||||||
return Math.sqrt(d * d + this.z * this.z);
|
return Math.sqrt(d * d + this.z * this.z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modules, "." can be used as separator for sub modules
|
// Modules, "." peut être utilisé comme un séparateur de sous modules.
|
||||||
module Geometry {
|
module Geometry {
|
||||||
export class Square {
|
export class Square {
|
||||||
constructor(public sideLength: number = 0) {
|
constructor(public sideLength: number = 0) {
|
||||||
@@ -133,12 +130,12 @@ module Geometry {
|
|||||||
|
|
||||||
var s1 = new Geometry.Square(5);
|
var s1 = new Geometry.Square(5);
|
||||||
|
|
||||||
// Local alias for referencing a module
|
// Alias local pour référencer un module
|
||||||
import G = Geometry;
|
import G = Geometry;
|
||||||
|
|
||||||
var s2 = new G.Square(10);
|
var s2 = new G.Square(10);
|
||||||
|
|
||||||
// Generics
|
// Génériques
|
||||||
// Classes
|
// Classes
|
||||||
class Tuple<T1, T2> {
|
class Tuple<T1, T2> {
|
||||||
constructor(public item1: T1, public item2: T2) {
|
constructor(public item1: T1, public item2: T2) {
|
||||||
@@ -151,14 +148,14 @@ interface Pair<T> {
|
|||||||
item2: T;
|
item2: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
// And functions
|
// Et fonctions
|
||||||
var pairToTuple = function<T>(p: Pair<T>) {
|
var pairToTuple = function<T>(p: Pair<T>) {
|
||||||
return new Tuple(p.item1, p.item2);
|
return new Tuple(p.item1, p.item2);
|
||||||
};
|
};
|
||||||
|
|
||||||
var tuple = pairToTuple({ item1:"hello", item2:"world"});
|
var tuple = pairToTuple({ item1:"hello", item2:"world"});
|
||||||
|
|
||||||
// Including references to a definition file:
|
// Inclure des références à un fichier :
|
||||||
/// <reference path="jquery.d.ts" />
|
/// <reference path="jquery.d.ts" />
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user