What is the difference between `mixed` and `any`?

Aton picture Aton · May 2, 2015 · Viewed 7.3k times · Source

The docs say:

  • mixed: the "supertype" of all types. Any type can flow into a mixed.
  • any: the "dynamic" type. Any type can flow into any, and vice-versa

What would be a case where mixed and any cannot be used interchangeably?

Answer

ekuusela picture ekuusela · May 4, 2015

The difference is the "vice-versa": any can flow into other types but mixed can not.

/* @flow */
var numeric:number = 0;
var looselyTyped:any;
var someType:mixed;

numeric = looselyTyped;
numeric = someType; //only this will throw a flow check error

From the docs you linked to:

It is worth calling out any specifically because of the special nature of this annotation. Use any to escape the static typing of Flow. In other words, if Flow is getting in your way, and you are absolutely convinced your program is type correct, you can silence the errors by annotating locations along the error paths with type any.