原始类型
JavaScript 有许多不同的原始类型 (MDN):
¥JavaScript has a number of different primitive types (MDN):
| 示例 | Flow 类型 | |
|---|---|---|
| 布尔值 | true 或 false | boolean |
| 字符串 | 'foo' | string |
| 数字 | 123 | number |
| 无效的 | null | null |
| 不明确的 | undefined | void |
| 符号 (ES2015 中的新功能) | Symbol('foo') | symbol |
| BigInts (ES2020 中新增) | 123n | bigint |
一些基本类型在语言中以字面量值的形式出现:
¥Some primitive types appear in the language as literal values:
1true;2"hello";33.14;4null;5undefined;63n;BigInts 和 Symbols 可以分别通过调用 BigInt 和 Symbol 来创建:
¥BigInts and Symbols can be created with calls to BigInt and Symbol, respectively:
1BigInt("2364023476023");2Symbol("hello");Flow 类型的字面量值是小写的(镜像 JavaScript 的 typeof 表达 的输出):
¥The Flow types of literal values are lowercase (mirroring the output of JavaScript's
typeof expression):
1function func(a: number, b: string, c: boolean, d: bigint) { /* ... */ }2
3func(3.14, "hello", true, 3n);有些字面量也可以用作 字面量类型:
¥Some literals can also be used as literal types:
1function acceptTwo(x: 2) { /* ... */ }2
3acceptTwo(2); // Works!4acceptTwo(1); // Error!一些原语也可以封装为对象:
¥Some primitives can also be wrapped as objects:
注意:你可能永远不想使用封装器对象变体。
¥NOTE: You probably never want to use the wrapper object variants.
1new Boolean(false);2new String("world");3new Number(42);封装对象的类型是大写的(与其构造函数相同):
¥Types for the wrapper objects are capitalized (the same as their constructor):
1function func(x: Number, y: String, z: Boolean) {2 // ...3}4
5func(new Number(42), new String("world"), new Boolean(false));这些封装对象很少使用。
¥These wrapper objects are rarely used.
布尔值
¥Booleans
布尔值是 JavaScript 中的 true 和 false 值。Flow 中的 boolean 类型接受这些值。
¥Booleans are true and false values in JavaScript. The boolean type in
Flow accepts these values.
1function acceptsBoolean(value: boolean) { /* ... */ }2
3acceptsBoolean(true); // Works!4acceptsBoolean(false); // Works!5
6acceptsBoolean("foo"); // Error!JavaScript 还可以将其他类型的值隐式转换为布尔值。
¥JavaScript can also implicitly convert other types of values into booleans.
if (42) {} // 42 => true
if ("") {} // "" => false
Flow 理解这些强制,并允许它们作为 if 语句的测试或其他条件上下文的一部分。
¥Flow understands these coercions and will allow them as part of an
if statement's test or other conditional contexts.
要将非布尔值显式转换为 boolean,可以使用 Boolean(x) 或 !!x。
¥To explicitly convert non-booleans to a boolean, you can use Boolean(x) or !!x.
1function acceptsBoolean(value: boolean) { /* ... */ }2
3acceptsBoolean(0); // Error!4
5acceptsBoolean(Boolean(0)); // Works!6acceptsBoolean(!!0); // Works!你可以使用 typeof 检查将 精炼 值转换为 boolean:
¥You can refine a value to boolean using a typeof check:
1function acceptsBoolean(value: boolean) { /* ... */ }2
3function func(value: mixed) {4 if (typeof value === 'boolean') {5 acceptsBoolean(value); // Works: `value` is `boolean`6 }7}请记住,boolean 和 Boolean 是不同的类型。
¥Remember that boolean and Boolean are different types.
boolean是像true或false这样的字面值,或者是像a === b这样的表达式的结果。¥A
booleanis a literal value liketrueorfalseor the result of an expression likea === b.Boolean是由全局new Boolean(x)构造函数创建的封装对象。你可能不想使用这个!¥A
Booleanis a wrapper object created by the globalnew Boolean(x)constructor. You probably don't want to use this!
数字
¥Numbers
JavaScript 中的数字字面量是浮点数,例如 42 或 3.14。JavaScript 也将 Infinity 和 NaN 视为数字。这些由 number 类型代表。JavaScript 也有一个单独的 BigInt 类型。
¥Number literals in JavaScript are floating point numbers, for example 42 or 3.14.
JavaScript also considers Infinity and NaN to be numbers.
These are represented by the number type. JavaScript also has a separate BigInt type.
1function acceptsNumber(value: number) { /* ... */ }2
3acceptsNumber(42); // Works!4acceptsNumber(3.14); // Works!5acceptsNumber(NaN); // Works!6acceptsNumber(Infinity); // Works!7
8acceptsNumber("foo"); // Error!9acceptsNumber(123n); // Error!你可以使用 typeof 检查将 精炼 值转换为 number:
¥You can refine a value to number using a typeof check:
1function acceptsNumber(value: number) { /* ... */ }2
3function func(value: mixed) {4 if (typeof value === 'number') {5 acceptsNumber(value); // Works: `value` is `number`6 }7}请记住,number 和 Number 是不同的类型。
¥Remember that number and Number are different types.
number是像42或3.14这样的字面值,或者是像parseFloat(x)这样的表达式的结果。¥A
numberis a literal value like42or3.14or the result of an expression likeparseFloat(x).Number是由全局new Number(x)构造函数创建的封装对象。你可能不想使用这个!¥A
Numberis a wrapper object created by the globalnew Number(x)constructor. You probably don't want to use this!
字符串
¥Strings
字符串是 JavaScript 中的 "foo" 值。Flow 中的 string 类型接受这些值。
¥Strings are "foo" values in JavaScript. The string type in Flow accepts these values.
1function acceptsString(value: string) { /* ... */ }2
3acceptsString("foo"); // Works!4acceptsString(`template literal`); // Works!5
6acceptsString(false); // Error!JavaScript 通过连接其他类型的值隐式地将它们转换为字符串。
¥JavaScript implicitly converts other types of values into strings by concatenating them.
"foo" + 42; // "foo42"
"foo" + {}; // "foo[object Object]"
当将字符串和数字连接到字符串时,Flow 仅接受它们。
¥Flow will only accept strings and numbers when concatenating them to strings.
1"foo" + "foo"; // Works!2"foo" + 42; // Works!3`foo ${42}`; // Works!4
5"foo" + {}; // Error!6"foo" + []; // Error!7`foo ${[]}`; // Error!你必须显式地将其他类型转换为字符串。你可以通过使用 String 函数或使用其他字符串化值的方法来完成此操作。
¥You must be explicit and convert other types into strings. You can do this by using the String function or using another method for stringifying values.
1"foo" + String({}); // Works!2"foo" + [].toString(); // Works!3"" + JSON.stringify({}) // Works!你可以使用 typeof 检查将 精炼 值转换为 string:
¥You can refine a value to string using a typeof check:
1function acceptsString(value: string) { /* ... */ }2
3function func(value: mixed) {4 if (typeof value === 'string') {5 acceptsString(value); // Works: `value` is `string`6 }7}请记住,string 和 String 是不同的类型。
¥Remember that string and String are different types.
string是像"foo"这样的字面量值,或者是像"" + 42这样的表达式的结果。¥A
stringis a literal value like"foo"or the result of an expression like"" + 42.String是由全局new String(x)构造函数创建的封装对象。你可能不想使用这个!¥A
Stringis a wrapper object created by the globalnew String(x)constructor. You probably don't want to use this!
null 和 undefined
¥null and undefined
JavaScript 有 null 和 undefined。Flow 将它们视为单独的类型:null 和 void(对于 undefined)。
¥JavaScript has both null and undefined. Flow treats these as separate
types: null and void (for undefined).
1function acceptsNull(value: null) { /* ... */ }2
3acceptsNull(null); // Works!4acceptsNull(undefined); // Error!5
6function acceptsUndefined(value: void) { /* ... */ }7
8acceptsUndefined(undefined); // Works!9acceptsUndefined(null); // Error!你可以使用相等性检查将 精炼 值转换为 null 或 void:
¥You can refine a value to null or void using equality checks:
1function acceptsNull(value: null) { /* ... */ }2
3function func(value: mixed) {4 if (value === null) {5 acceptsNull(value); // Works: `value` is `null`6 }7}1function acceptsUndefined(value: void) { /* ... */ }2
3function func(value: mixed) {4 if (value === undefined) {5 acceptsUndefined(value); // Works: `value` is `void`6 }7}null 和 void 也出现在其他类型中:
¥null and void also appear in other types:
也许类型
¥Maybe types
也许类型 用于值可选的地方,你可以通过在类型前面添加问号(例如 ?string 或 ?number)来创建它们。
¥Maybe types are for places where a value is optional and you can create them by
adding a question mark in front of the type such as ?string or ?number.
?T 相当于 T | null | void。
¥?T is equivalent to T | null | void.
1function acceptsMaybeString(value: ?string) { /* ... */ }2
3acceptsMaybeString("bar"); // Works!4acceptsMaybeString(undefined); // Works!5acceptsMaybeString(null); // Works!6acceptsMaybeString(); // Works!为了细化,value == null 精确检查 null 和 undefined。
¥To refine, value == null checks exactly for both null and undefined.
阅读 也许输入文档 了解更多详细信息。
¥Read the maybe type docs for more details.
可选对象属性
¥Optional object properties
对象类型可以具有可选属性,其中属性名称后面带有问号 ?。
¥Object types can have optional properties where a question mark ? comes after
the property name.
{propertyName?: string}
除了它们的设置值类型之外,这些可选属性可以是 void 或完全省略。然而,它们不可能是 null。
¥In addition to their set value type, these optional properties can either be
void or omitted altogether. However, they cannot be null.
1function acceptsObject(value: {foo?: string}) { /* ... */ }2
3acceptsObject({foo: "bar"}); // Works!4acceptsObject({foo: undefined}); // Works!5acceptsObject({}); // Works!6
7acceptsObject({foo: null}); // Error!可选功能参数
¥Optional function parameters
函数可以具有可选参数,其中参数名称后面带有问号 ?。
¥Functions can have optional parameters where a question mark ? comes after
the parameter name.
function func(param?: string) { /* ... */ }
除了它们的设置类型之外,这些可选参数可以是 void 或完全省略。然而,它们不可能是 null。
¥In addition to their set type, these optional parameters can either be void
or omitted altogether. However, they cannot be null.
1function acceptsOptionalString(value?: string) { /* ... */ }2
3acceptsOptionalString("bar"); // Works!4acceptsOptionalString(undefined); // Works!5acceptsOptionalString(); // Works!6
7acceptsOptionalString(null); // Error!默认值的函数参数
¥Function parameters with defaults
函数参数也可以有默认值。这是 ES2015 的一个特性。
¥Function parameters can also have defaults. This is a feature of ES2015.
function func(value: string = "default") { /* ... */ }
除了它们的设置类型之外,默认参数也可以是 void 或完全省略。然而,它们不可能是 null。
¥In addition to their set type, default parameters can also be void or omitted
altogether. However, they cannot be null.
1function acceptsOptionalString(value: string = "foo") { /* ... */ }2
3acceptsOptionalString("bar"); // Works!4acceptsOptionalString(undefined); // Works!5acceptsOptionalString(); // Works!6
7acceptsOptionalString(null); // Error!符号
¥Symbols
符号是在 JavaScript 中使用 Symbol() 创建的。Flow 对符号有基本的支持,使用 symbol 类型。
¥Symbols are created with Symbol() in JavaScript. Flow has basic support for symbols, using the symbol type.
1function acceptsSymbol(value: symbol) { /* ... */ }2
3acceptsSymbol(Symbol()); // Works!4acceptsSymbol(Symbol.isConcatSpreadable); // Works!5
6acceptsSymbol(false); // Error!你可以使用 typeof 检查将 精炼 值转换为 symbol:
¥You can refine a value to symbol using a typeof check:
1function acceptsSymbol(value: symbol) { /* ... */ }2
3function func(value: mixed) {4 if (typeof value === 'symbol') {5 acceptsSymbol(value); // Works: `value` is `symbol`6 }7}长整型数字
¥BigInts
BigInts 可用于表示任意精度的整数。换句话说,它们可以存储太大而无法存储为 number 的整数。
¥BigInts can be used to represent integers of arbitrary precision. In other words, they can store integers which are too large to store as a number.
bigint 字面量只是 number 字面量加上 n 后缀。
¥A bigint literal is just a number literal along with an n suffix.
请注意,bigint 和 number 是不兼容的类型。也就是说,不能在需要 number 的地方使用 bigint,反之亦然。
¥Note that bigint and number are incompatible types. That is, a bigint cannot be used where a number is expected, and vice versa.
1function acceptsBigInt(value: bigint) { /* ... */ }2
3acceptsBigInt(42n); // Works!4acceptsBigInt(42); // Error!你可以使用 typeof 检查将 精炼 值转换为 bigint:
¥You can refine a value to bigint using a typeof check:
1function acceptsBigInt(value: bigint) { /* ... */ }2
3function func(value: mixed) {4 if (typeof value === 'bigint') {5 acceptsBigInt(value); // Works: `value` is `bigint`6 }7}