Primitive Types
JavaScript has a number of different primitive types (MDN):
| Example | Flow type | |
|---|---|---|
| Booleans | true or false | boolean |
| Strings | 'foo' | string |
| Numbers | 123 | number |
| Null | null | null |
| Undefined | undefined | void |
| Symbols (new in ES2015) | Symbol('foo') | symbol |
| BigInts (new in ES2020) | 123n | bigint |
Some primitive types appear in the language as literal values:
1true;2"hello";33.14;4null;5undefined;63n;BigInts and Symbols can be created with calls to BigInt and Symbol, respectively:
1BigInt("2364023476023");2Symbol("hello");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! 4:11-4:11: Cannot call `acceptTwo` with `1` bound to `x` because number literal `1` [1] is incompatible with number literal `2` [2]. [incompatible-type]
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
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! 6:16-6:20: Cannot call `acceptsBoolean` with `"foo"` bound to `value` because string [1] is incompatible with boolean [2]. [incompatible-type]
JavaScript can also implicitly convert other types of values into booleans.
if (42) {} // 42 => true
if ("") {} // "" => false
Flow understands these coercions and will allow them as part of an
if statement's test or other conditional contexts.
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!3:16-3:16: Cannot call `acceptsBoolean` with `0` bound to `value` because number [1] is incompatible with boolean [2]. [incompatible-type]
You can refine a value to boolean using a typeof check:
1function acceptsBoolean(value: boolean) { /* ... */ }2
3function func(value: unknown) {4 if (typeof value === 'boolean') {5 acceptsBoolean(value); // Works: `value` is `boolean`6 }7}Remember that boolean and Boolean are different types.
- A
booleanis a literal value liketrueorfalseor the result of an expression likea === b. - A
Booleanis a wrapper object created by the globalnew Boolean(x)constructor. You probably don't want to use this!