Files
test-repo/tests/refinements/typeof.js
T
Christopher Chedeau cf45afba61 Add testing
- This brings in the flow test suite that contains a ton of JavaScript parsing edge cases
- This creates snapshot tests using the pretty printer for all of them
- If uncomment `RUN_AST_TESTS` line in `tests/run_specs.js`, it checks ast(pretty_print(x)) == ast(x). Right now, "178 failed, 197 passed, 375 of 377 total". So half of the tests are not passing, most of them are crashes and many of the rest are subtle issues.
2016-12-23 19:51:53 +00:00

65 lines
1.2 KiB
JavaScript

/* @flow */
function foo(x: bool | number) {
if (typeof x === "boolean") {
x[0]; // error for boolean, not number
}
}
function bar(): number {
var x = null;
if (typeof x === "object") {
return x; // error, null
}
return 0;
}
/* refining globals */
function fn0() {
if (typeof BAZ !== 'undefined' &&
typeof BAZ.stuff === 'function') {
BAZ.stuff(123);
}
BAZ.stuff(123); // error, refinement is gone
}
function fn1() {
BAZ.stuff; // error, could be undefined
if (typeof BAZ !== 'undefined' &&
typeof BAZ.stuff === 'function') {
BAZ.stuff(123); // ok
BAZ.stuff(123); // error, refinement is gone
}
}
function anyfun(x: number | Function): number {
if (typeof x === "function") {
return 0;
}
return x; // OK, x refined to `number`
}
function anyobj(x: number | Object): number {
if (typeof x === "object") {
return 0;
}
return x; // OK, x refined to `number`
}
function testInvalidValue(x: mixed) {
if (typeof x === "foo") { // error
return 0;
}
}
function testTemplateLiteral(x: string | number) {
if (typeof x === `string`) {
return x.length;
}
}
function testInvalidTemplateLiteral(x: string | number) {
if (typeof x === `foo`) { // error
return 0;
}
}