#How do I pass a generic type parameter to a higher order function?

7 messages · Page 1 of 1 (latest)

wide pasture
#

For example, see the TypeScript error in this small playground link:

[edit - see bot link below]

lavish bluffBOT
#
zamiel#0

Preview:```ts
import {produce} from "immer"

function myGenericFunction<T extends number>(
arg1: T
) {}

interface MyState<T extends number> {
foo: T
}

const reducer = produce(
myGenericFunction,
{} as MyState
)```

drowsy wigeon
#

you'll need a name for the type parameter within the function, which in this case might go something like:

lavish bluffBOT
#
mkantor#0

Preview:ts ... const reducer = <T extends number>() => produce(myGenericFunction<T>, {} as MyState<T>)() ...

wide pasture
#

Perfect, thank you!

drowsy wigeon
#

(i'm not sure which signature(s) you actually need since produce is overloaded. that may not be exactly right, but should communicate the general idea)

#

no problem!