...or why we actually decided to have two type checkers instead of one...
class Cat {
voice = () => "nope.";
}
class Dog {
voice = () => "woof!";
}
class NuclearSafetyInspector {
voice = () => "D'oh!";
}
function say(speaker) {
console.log(speaker.voice());
}
say(new Dog());
say(new Cat());
say(new NuclearSafetyInspector());class Cat {
voice = () => "nope.";
}
class Dog {
voice = () => "woof!";
}
class NuclearSafetyInspector {
voice = () => "D'oh!";
}
function say(speaker) {
console.log(speaker.voice());
}
say(new Dog());
say(new Cat());
say(new NuclearSafetyInspector());Compiler Error!
interface CanSpeak {
voice(): string;
}
class Cat implements CanSpeak {
voice = () => "nope.";
}
class Dog implements CanSpeak {
voice = () => "woof!";
}
class NuclearSafetyInspector implements CanSpeak {
voice = () => "D'oh!";
}
function say(speaker: CanSpeak): void {
console.log(speaker.voice());
}
say(new Dog());
say(new Cat());
say(new NuclearSafetyInspector());
Static Type Checker
Created By Facebook
Has Type Inference
Plain JS is Valid Flow
// @flow
class Cat {
voice = () => "meow!";
}
class NuclearSafetyInspector {
voice = () => "D'oh!";
}
function say(speaker) {
console.log(speaker.voice());
}
say(new Cat());
say(new NuclearSafetyInspector());// @flow
class Cat {
voice = () => "nope.";
}
class NuclearSafetyInspector {
voice = () => "D'oh!";
}
class Empty {}
function say(speaker) {
console.log(speaker.voice());
}
say(new Cat());
say(new NuclearSafetyInspector());
say(new Empty());// @flow
class Cat {
voice = () => "nope.";
}
class NuclearSafetyInspector {
voice = () => "D'oh!";
}
class Empty {}
function say(speaker) {
console.log(speaker.voice());
}
say(new Cat());
say(new NuclearSafetyInspector());
say(new Empty());Strongly Typed Programming Language
Created By Microsoft
Has Type Inference
Plain JS is Valid TS
type CanSpeak = {
voice(): string,
}
class Cat {
voice = () => "meow!";
}
class NuclearSafetyInspector {
voice = () => "D'oh!";
}
function say(speaker: CanSpeak): void {
console.log(speaker.voice());
}
say(new Cat());
say(new NuclearSafetyInspector());Any
Object, Enum
Union, Intersection
Mixin
Functions
Generics
Optional
Casting
Inference
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
and much more...
const arr: number[] = [];
const a: number = arr[0];// Command pattern imposibility
class A {
method1() {}
method2() {}
exec(command: "method1" | "method2") {
this[command](); //FlowError
// (this: any)[command]();
}
}class Animal {}
class Dog extends Animal { bark() {} }
class Cat extends Animal { meow() {} }
const cats: Array<Cat> = [new Cat(), new Cat()];
const animals: Array<Animal> = cats;
animals[1] = new Dog();
cats[1].meow(); //TypeError!class Animal {}
class Dog extends Animal { bark() {} }
class Cat extends Animal { meow() {} }
const cats: Array<Cat> = [new Cat(), new Cat()];
const animals: Array<Animal> = cats;
animals[1] = new Dog();
cats[1].meow(); //TypeError!(null as any).method(); //TS
(null: any).method(); //Flowtype NonExact = {
some: number;
};
const a: NonExact = { some: 1, a: "2" };
type Exact = {|
some: number;
|};
const b: Exact = { some: 1, a: "2" }; // Cause Errortype NonExact = {
some: number;
};
const a: NonExact = { some: 1, a: "2" };
type Exact = {|
some: number;
|};
const b: Exact = { some: 1, a: "2" }; // Cause Error