I have a function:
const r = (array, function) => { some stuff }
However array
and function
arguments can be anything , but array must be typeof array and function must be typeof function .
However, the array
can be array of anything - any[]
anf function
can be anything function(...r: any): any
.
How can I type that r
function to accept generic array
and function
arguments, but the type definition has to be passed when calling it?
Eg Im calling it somewhere in my app:
r([1,2,3], (r) => r + 2)
Thanks!
This is my interpretation of what your're looking for
type Fn<T> = (arg: T) => T
type R = <T>(array: T[], fn: Fn<T>) => T
const r: R = (array, fn) => {
//some stuff
}
I prefer to split the type declaration and its usage.
尝试这个:
const r = <T>(array: T[], fn: (argument: T) => T) => { /* ... */}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.