Skip to main content

混入

mixed所有类型的超类型。所有值均为 mixed。然而,这意味着在不细化到某些更具体的类型的情况下,只允许对其进行很少的操作。这是因为 mixed 上的有效操作必须对所有类型都有效。

¥mixed is the supertype of all types. All values are mixed. However, this means that very few operations are permitted on it, without refining to some more specific type. That's because the valid operations on mixed must be valid for all types.

一般来说,程序有几种不同的类型:

¥In general, programs have several different categories of types:

单一类型:

¥A single type:

这里输入的值只能是 number

¥Here the input value can only be a number.

1function square(n: number) {2  return n * n;3}

一组不同的可能类型:

¥A group of different possible types:

这里的输入值可以是 stringnumber

¥Here the input value could be either a string or a number.

1function stringifyBasicValue(value: string | number) {2  return '' + value;3}

一种类型基于另一种类型:

¥A type based on another type:

这里的返回类型将与传递给函数的任何值的类型相同。

¥Here the return type will be the same as the type of whatever value is passed into the function.

1function identity<T>(value: T): T {2  return value;3}

这三种是最常见的类型类别。它们将构成你将要编写的大多数类型。

¥These three are the most common categories of types. They will make up the majority of the types you'll be writing.

然而,还有第四类。

¥However, there is also a fourth category.

任意类型,可以是任何东西:

¥An arbitrary type that could be anything:

这里传入的值是未知类型,它可以是任何类型,并且该函数仍然可以工作。

¥Here the passed in value is an unknown type, it could be any type and the function would still work.

1function getTypeOf(value: mixed): string {2  return typeof value;3}

这些未知类型不太常见,但有时仍然有用。

¥These unknown types are less common, but are still useful at times.

你应该用 mixed 来表示这些值。

¥You should represent these values with mixed.

一切都进去了,什么也没有出来

¥Anything goes in, Nothing comes out

mixed 将接受任何类型的值。字符串、数字、对象、函数 - 任何东西都可以。

¥mixed will accept any type of value. Strings, numbers, objects, functions– anything will work.

1function stringify(value: mixed) {2  // ...3}4
5stringify("foo");6stringify(3.14);7stringify(null);8stringify({});

当你尝试使用 mixed 类型的值时,你必须首先弄清楚实际类型是什么,否则最终会出现错误。

¥When you try to use a value of a mixed type you must first figure out what the actual type is or you'll end up with an error.

1function stringify(value: mixed) {2  return "" + value; // Error!3}4
5stringify("foo");

相反,你必须通过 精制 确保该值是某种类型。

¥Instead you must ensure the value is a certain type by refining it.

1function stringify(value: mixed) {2  if (typeof value === 'string') {3    return "" + value; // Works!4  } else {5    return "";6  }7}8
9stringify("foo");

由于 typeof value === 'string' 检查,Flow 知道 value 只能是 if 语句内的 string。这被称为 细化

¥Because of the typeof value === 'string' check, Flow knows the value can only be a string inside of the if statement. This is known as a refinement.

对比 any

¥Versus any

mixed 是安全的,而 any 则不安全。两者都接受所有值,但 any 也不安全地允许所有操作。

¥mixed is safe, while any is not. Both accept all values, but any also unsafely allows all operations.

对比 empty

¥Versus empty

mixedempty 相反:

¥mixed is the opposite of empty:

  • 一切都是 mixed,但在没有首先细化为特定类型的情况下,很少允许对其进行操作。它是所有类型的超类型。

    ¥Everything is a mixed, but few operations are permitted on it without first refining to a specific type. It is the supertype of all types.

  • 没有什么是 empty,但允许对其进行任何操作。它是所有类型的子类型。

    ¥Nothing is empty, but any operation is permitted on it. It is the subtype of all types.