In the following function definition:
function foo(a: number = 42): number {return a+1;}
… what are the semantics of the a: number
annotation?
Is it saying that variable a will always have a value inside the body of the function or is it saying that client programmers should always supply a value when they make the call?
I've noticed that both the below code snippets type-check with no errors (with flow-bin 0.57.3):
function foo(a: number = 42): number {return a+1;}
type FooT= (a: number)=> number
(foo: FooT)
foo();
(try it here)
function foo(a: ?number = 42): number {return a+1;}
type FooT = (a: ?number)=> number
(foo: FooT)
foo();
(try it here)
What is the suggested way to annotate in such a case?
My preference is with way #2 as the client programmer only has to look at the definition of the FooT
type to realize that the parameter is optional. This allows me to tell users of my
library: "simply look at the type of the function (FooT
)".
Whereas with way #1 I have to tell them "the type of the function (FooT
) seems to suggest that an argument is required, but in fact it isn't because, see, if you look at the implementation, a default value is supplied".
So, which snippet is more idiomatic?
Note that there is an answer to a related question that seems to suggest that it is possible to annotate the type as mandatory in the implementation of a function and as optional in the declaration. But this doesn't seem to work in this case. E.g. the following doesn't type-check:
function foo(a: number = 42): number {return a+1;}
type FooT = (a: ?number)=> number
(foo: FooT)
foo();
(try it here)
Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Default parameter values must appear on the declaration, since that is the only thing that the caller sees.
Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed. function foo(a, b) { a = typeof a !== 'undefined' ? a : 42; b = typeof b !==
You can go with:
function foo(a: number = 42): number {return a+1;}
type FooT = (a?: number)=> number
(foo: FooT)
foo();
Pay attention to ?
position in type definition:
(a?: number)
- optional parameter
vs
(a: ?number)
- maybe type
The only difference between the two is that the optional parameter can't be null, just like a parameter with default value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With