Skip to content

Instantly share code, notes, and snippets.

@susisu
Last active July 24, 2017 01:11
Show Gist options
  • Save susisu/02d7b10746152a4a2531358feed4323a to your computer and use it in GitHub Desktop.
Save susisu/02d7b10746152a4a2531358feed4323a to your computer and use it in GitHub Desktop.
Flaws of Flow
type Inconsistent = { foo: string } & { foo: number };
// const obj: Inconsistent = { foo: 123 }; // error, consistently
function trick<O: {}, K: string, V>(obj: O, key: K, val: V): O & { [K]: V } {
obj[key] = val;
return obj;
}
const obj = trick({ foo: "bar" }, "foo", 123);
(obj: Inconsistent); // !
(obj.foo: string); // ok
// (obj.foo: number); // error, why?
obj.foo.charAt(0); // runtime error, obj.foo is a number
// @flow
class Animal {}
class Cat extends Animal {
nyan() {
console.log("nyan");
}
}
class Serval extends Cat {
sugoi() {
console.log("sugoi");
}
}
interface Src<+T> {
read(): T,
write(val: T): void // !?
}
const src: Src<Cat> = {
val: new Cat(),
read(): Cat {
return this.val;
},
write(val: Cat) {
this.val = val;
}
};
(src.read(): Animal);
(src.read(): Cat);
// (src.read(): Serval); // error
// src.write(new Animal()); // error
src.write(new Cat());
src.write(new Serval());
(src: Src<Animal>); // !?
(src: Src<Cat>);
// (src: Src<Serval>); // error
(src: Src<Animal>).write(new Animal()); // :innocent:
(src.read(): Cat).nyan(); // runtime error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment