[英]What is the difference between `mixed` and `any`?
mixed
: the "supertype" of all types.mixed
:所有类型的“超类型”。 Any type can flow into amixed
.任何类型都可以流入
mixed
。any
: the "dynamic" type.any
:“动态”类型。 Any type can flow intoany
, and vice-versa任何类型的可流入
any
,并且反之亦然
What would be a case where mixed
and any
cannot be used interchangeably? 什么是
mixed
和any
不能互换使用的情况?
The difference is the "vice-versa": any
can flow into other types but mixed
can not. 区别在于“反之亦然”:
any
可以流入其他类型但mixed
不能。
/* @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.
使用any来逃避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.
换句话说,如果Flow阻碍了您,并且您绝对确信您的程序类型正确,则可以通过在类型为any的错误路径上注释位置来消除错误。
"Any" supports covariance and contravariance. “任何”支持协方差和逆变。 That's because "any" is a super-type and a subtype of all types.
那是因为“any”是所有类型的超类型和子类型。
Hence this works, 因此,这工作,
let genericVariable: any = 20;
let numericVariable: number;
genericVariable = numericVariable; // No error
numericVariable = genericVariable; // No error
mixed supports covariance only. 混合仅支持协方差。 It's a super-type and not a sub type of all types.
它是超类型而不是所有类型的子类型。
let genericVariable: mixed = 20;
let numericVariable: number;
numericVariable = genericVariable; // This shows error
genericVariable = numericVariable; // This works fine.
Covariance - Generic type (parent) could be substituted by special type (child) 协方差 - 通用类型(父)可以用特殊类型(子)代替
Contravariance - Special type (child) could be substituted by Generic type (parent). 逆变 - 特殊类型(子)可以用泛型类型(父级)代替。 This is a problem, unless protected by certain conventions.
这是一个问题,除非受某些约定的保护。
When flow sees any
that means you can use any type. 当流程看到
any
意味着你可以使用任何类型。 The program is indifferent as to the parameter's type, and it will not try to infer result type. 该程序对参数的类型无动于衷,并且不会尝试推断结果类型。 So result type will be
any
also. 所以结果类型将是
any
还。
For example, the following code will not report any errors: 例如,以下代码不会报告任何错误:
// @flow
function add(one: any, two: any): number {
return one + two;
}
add(1, 2); // Works.
add("1", "2"); // Works.
add({}, []); // Works.
But "mixed" type should be processed someway to infer actual type. 但是应该以某种方式处理“混合”类型来推断实际类型。
// @flow
function stringify(value: mixed) {
// $ExpectError
return "" + value; // Error!
}
stringify("foo");
Instead you must ensure the value is a certain type by refining it. 相反,你必须通过改进它来确保价值是某种类型。
// @flow
function stringify(value: mixed) {
if (typeof value === 'string') {
return "" + value; // Works!
} else {
return "";
}
}
stringify("foo");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.