I am having trouble understanding some functions, and their interfaces. IE:
ReadonlyArray<unknown>.map(
callbackfn: (value: unknown, index: number, array: unknown[]) => unknown,
thisArg?: any): unknown[]
This is what WebStorm displays when I hover over a .map() function. I understand that .map() takes a specified callback function as a parameter, but what do => unknown, and thisArg?: any represent?
Likewise when I hover over a forEach() function, the popup looks like this:
ReadonlyArray<unknown>.forEach(
callbackfn: (value: unknown, index: number, array: unknown[]) => void,
thisArg?: any): void
forEach also takes a callback function as a parameter, but what does => void, thisArg?: any): void represent?
// the `.map` function is member of the generic type ReadonlyArray<unknown>. Where unknown is the type of the elements of the array
ReadonlyArray<unknown>.map(
// first argument is a callback function. The first argument of the callback is value, of type unknown, the second is index of type number, and so on
callbackfn: (value: unknown, index: number, array: unknown[])
// this parts means that the callback should return a value of type unknown
=> unknown,
// .map takes a second arguments called thisArg of any type. The ? means that the argument is optional
thisArg?: any)
// .map returns an array of type unknown
: unknown[]
The type is unknown because it depends on the type of the elements present in the array on which .map is applied. If it is an array of object, the unknown type will be object
Similarly, forEach takes a first argument which is a callback. But the callback returns nothing, hence you have => void.