Can someone explain the difference between:
function foo(bar: ?string) {
console.log(bar);
}
and:
function foo(bar?: string) {
console.log(bar);
}
When to use one over the other?
Can someone explain the difference between:
function foo(bar: ?string) {
console.log(bar);
}
and:
function foo(bar?: string) {
console.log(bar);
}
When to use one over the other?
Basically
bar: ?string
accepts a string, null or void:
foo("test");
foo(null);
foo()
While
bar?: string
Accepts only a string or void:
foo("test");
foo();
As passing null instead of a string is somewhat senseless, theres no real life difference between them.
bar?: ?string
somewhere, when would you use that? Or does that make no sense?
type Styles = { fontName?: ?string; }
{ prop?: foo }
is the "optional property syntax" which I'm guessing is a specific usage of the maybe
syntax
May 20, 2019 at 2:30
?string
(maybe type) means that bar
property can be string
aswell as null
and void
.
bar?
means that this property is optional.
More info: https://flow.org/en/docs/types/primitives/