I'm looking at these two function declarations:
function f1(chainFn: (fn: Function) => void = null) {
}
function f2(): (...args: any[]) => (cls: any) => any {
}
I don't really understand what the following parts define:
// function that accepts `fn` of Function type by returns what?
(fn: Function) => void = null
// function that accepts an array of arguments and returns a function that returns a result of `any` type?
(...args: any[]) => (cls: any) => any
Can anyone please elaborate and possible provide examples of concrete implementations?
Introduction to TypeScript void type The void type denotes the absence of having any type at all. It is a little like the opposite of the any type. Typically, you use the void type as the return type of functions that do not return a value.
TypeScript Data Type - Void Similar to languages like Java, void is used where there is no data. For example, if a function does not return any value then you can specify void as return type. Example: void.
Reasons you might add : void : Improves clarity - other devs don't have to read the method body to see if it returns anything. Safer - if you e.g. move code from another function into this one that has a return expr; statement in it, TypeScript will flag this mistake.
JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.
() => void
Is the type of a function which takes no arguments and does not return anything.
() => void = null
Is not a type at all.
function f(g: () => void = null) { ... }
Is a function f
which takes another function, g
of type () => void
, as an argument and provides a default value of null
for that argument if it is not provided. This also renders the argument optional.
I would like to add that using null
as the default value of an optional argument is a terrible practice because JavaScript passes undefined
not null
for unspecified arguments and there's no reason to change this behaviour as doing so is surprising. In such a case it is preferable to either write
// g is optional and will be undefined if not provided
f(g?: () => void) { ... }
or to write
// g is optional and will be a no-op if not provided.
f(g: () => void = () => {}) { ... }
The first function f1
accepts an argument chainFn
and it is a function that as a parameter accepts a function fn
and does not return anything => void
, also this parameter chainFn
is optional = null
with runtime default value of undefined.
The second function f2
does not accept any arguments and returns a function. That function accepts open parameters list of any type ...args:any[]
(you can call it using comma separated arguments var r = f2(); r(1,2,3,4);
) and it returns a function that accepts any type as parameters and returns something.
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