basically, in typescript i can do:
interface MyObjectType {
a: number;
b?: number;
}
const obj: MyObjectType = {
a: 1,
// b is left off
};
console.log(obj.b + 5); // NaN
and it type-checks just fine. this is because in typescript you have to remember to do != null on anything you access that can be null. meanwhile, in flow:
type MyObjectType = {
a: number;
b?: number;
}
const obj: MyObjectType = {
a: 1,
}
console.log(obj.b * 5);
gives an error: "undefined: This type is incompatible with number." basically, it's saying "well, b
can be undefined, but there's no way you'd want to use an undefined reference with arithmetic, so you have to make sure that b
actually exists." if we add a null/undefined check, this passes fine:
type MyObjectType = {
a: number;
b?: number;
}
const obj: MyObjectType = {
a: 1,
}
if (obj.b != null) {
console.log(obj.b * 5);
}