Files
test-repo/tests/flow/refinements/mixed.js
T
Christopher Chedeau 3375cfe605 Move tests around (#454)
* new_tests

* move_all_clobbered_tests

* remove all the tests that no longer exist

* re-run flow tests

* Move all the flow tests to tests/flow and prettier to tests/

* Move prettier tests to their own folders

* Add jsfmt files

* run prettier snapshot tests
2017-01-24 14:35:37 -08:00

122 lines
2.1 KiB
JavaScript

/* @flow */
function takesNumber(x: number) {}
function takesString(x: string) {}
function num(x: mixed) {
if (typeof x === "number") {
takesString(x); // error
(!x: false); // error: we don't know the truthiness of x
}
if (typeof x === "number" && x) {
(!x: false); // ok
}
if (x && typeof x === "number") {
(!x: false); // ok
}
}
function str(x: mixed) {
if (typeof x === "string") {
takesNumber(x); // error
(!x: false); // error: we don't know the truthiness of x
}
if (typeof x === "string" && x) {
(!x: false); // ok
}
if (x && typeof x === "string") {
(!x: false); // ok
}
}
function bool(x: mixed) {
if (typeof x === "boolean") {
takesString(x); // error
(x: true); // error: we don't know the truthiness of x
}
if (typeof x === "boolean" && x) {
(x: true); // ok
}
if (x && typeof x === "boolean") {
(x: true); // ok
}
}
function fun(x: mixed) {
if (typeof x === "function") {
takesString(x); // error
}
}
function obj0(x: mixed) {
if (typeof x === "object") {
takesString(x); // error
}
}
function obj1(x: mixed) {
if (Array.isArray(x)) {
takesString(x); // error
}
}
function undef(x: mixed) {
if (typeof x === "undefined") {
takesString(x); // error
}
}
function null_(x: mixed) {
if (x === null) {
takesString(x); // error
}
}
function maybe(x: mixed) {
if (x == null) {
takesString(x); // error
}
}
function true_(x: mixed) {
if (x === true) {
takesString(x); // error
}
}
function false_(x: mixed) {
if (x === false) {
takesString(x); // error
}
}
function obj2(x: mixed) {
if (typeof x === "object") {
(x: { [key: string]: mixed } | null);
if (x !== null) {
(x['foo']: string); // error, mixed
}
}
}
function obj2(x: mixed) {
if (typeof x === "object" && x) {
(x: Object);
}
if (x && typeof x === "object") {
(x: Object);
}
if (x != null && typeof x === "object") {
(x: Object);
}
if (x !== null && typeof x === "object") {
(x: Object);
}
}
function arr0(x: mixed) {
if (Array.isArray(x)) {
takesString(x[0]); // error
}
}