Loïc TRUCHOT
JavaScript Fullstack Expert & Senior web developer
CC-BY-NC-4.0 / Septembre 2019 / Loïc TRUCHOT
while discovering JavaScript ?
const playerName: string = 'Enzo';
function stopGame(player: string): boolean {
console.log(`Game Over ${player}`);
return false;
}
const isRunning: boolean = stopGame(playerName);
Before the = after the :
const isValid = "3";
let familyName;
let age;
age = 8;
const isMarried = false;
let isAuth;
let welcomeSentence;
welcomeSentence = "Bienvenue";
const lang = "fr";
Ajouter des types à un projet existant
const multiply = (x: number, y: number): number => x*y;
let antialiasing: 'x2' | 'x4' | 'x8' = 'x8';
// antialiasing = 'x16';
function chooseDifficulty(difficutly?: 'easy' | 'ironman'): 1 | 2 {
return difficutly ? 1 : 2;
}
// chooseDifficulty('ironmann');
// optional arg
chooseDifficulty();
// be DRY we the Type keyword (alias)
type aa = 'x2' | 'x4' | 'x8';
let myDifficulty: aa = 'x8';
const setAA = (level: aa): void => {
if (level === 'x8') {
window.cancelAnimationFrame(0);
}
};
type Jour = "lundi" | "mardi" | "mercredi" | number;
const j2: Jour = "mardi";
const creature = {
nom : "Basilic",
description : `Le basilic est une bête légendaire,
souvent présenté comme un reptile`,
id : 1234 ,
image : "Basilisk_aldrovandi.jpg",
pouvoirs : ["empoisonnement", "petrification"]
}
const creatures = [creature, creature];
// what types ?
Finally a real class !
+ protected, readonly, abstract, static...
abstract class Animal {
protected name: string;
constructor(theName: string) { this.name = theName; }
getName() {
return this.name;
}
}
class Rhino extends Animal {
private readonly isStrong: true;
constructor() { super("Rhino"); }
}
class Employee {
public name: string;
constructor(theName: string) { this.name = theName; }
}
let animal: Animal;
let rhino: Rhino = new Rhino();
let employee: Employee = new Employee("Bob");
console.log(rhino.getName());
animal = rhino;
animal = employee; // Error: 'Animal' and 'Employee' are not compatible
Write a class, matching the following conditions:
export type Player = {
name: string;
age: number;
};
export type PlayerService = {
id: string;
getPlayer: (s: string) => Player;
};
export interface IRequestService extends IService {
get: (s: string) => string;
}
export interface IService {
id: string;
}
export interface IGame {
players: Player[];
playerService: PlayerService;
requestService: IRequestService;
isRunning?: boolean;
history: IGameData;
}
interface IGameData {
[key: string]: string;
}
type Identity<T> = (arg: T) => T;
const sameNumber: Identity<number> = (x) => x;
const n: number = sameNumber(3);
const s: string = sameNumber("test");
Stole "annotation" from Python and transform it for JavaScript
const log = (
target: any,
key: string,
descriptor: PropertyDescriptor
): any => {
const method = descriptor.value;
descriptor.value = function (...args: any[]): any {
const s = JSON.stringify;
const r = method.apply(this, args);
console.log(`Call: ${key}(${s(args)}) => ${s(r)}`);
return r;
}
return descriptor;
}
class Employee {
public name: string;
constructor(theName: string) { this.name = theName; }
@log
pay (salary: number) {
return "your salary is:" + salary.toString();
}
}
let employee: Employee = new Employee("Bob");
employee.pay(10000);
// Call: pay([10000]) => "your salary is:10000"
Clean syntaxe for an hidden wrapper for
THANK'S EVERYONE
It's finally done ! See you soon
By Loïc TRUCHOT
Discovering computer science basis with some JS examples