How do I define the type of the config
parameter given that it has a default value?
function (config = {}) {};
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 !==
Default Parameter Data TypesAny primitive value or object can be used as a default parameter value.
In the function definition f(x) = x*x the variable x is a parameter; in the function call f(2) the value 2 is the argument of the function. Loosely, a parameter is a type, and an argument is an instance. A parameter is an intrinsic property of the procedure, included in its definition.
Python has a different way of representing syntax and default values for function arguments. 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.
function f(config: Object = {}) {}
Or, more generally:
function f(p: T = v) {}
where T
is a type, and v
is a value of type T
.
Interestingly, the type of function f
is (p?: T): void
. That is, Flow understands that providing a default value makes the parameter optional. You don't need to explicitly make the parameter type optional—although it doesn't hurt.
When writing declare function
statement in a .js.flow
file, you can't include the default value; it will cause an error. So you must explicitly declare that the parameter is optional:
declare function f(p?: T): void;
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