Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created December 19, 2015 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unscriptable/653d820ce78f38d2a76a to your computer and use it in GitHub Desktop.
Save unscriptable/653d820ce78f38d2a76a to your computer and use it in GitHub Desktop.
I thought classes were safe :(
'use strict';
/*@flow*/
class Square {
/*::
x: number;
y: number;
w: number;
*/
constructor (x/*: number*/, y/*: number*/, w/*: number*/) {
this.x = x;
this.y = y;
this.w = w;
}
}
const area
/*: (square: Square) => number*/
= (square) => {
if (!(square instanceof Square)) {
throw new TypeError('oops, not a Square!');
}
return square.w * square.w;
};
// This type-checks and runs without error
area(new Square(1, 2, 6));
// This typechecks and throws a TypeError.
area ({ x:1, y:2, w:6, foo:'bar' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment