#Cannot get return type of callback returned from generic function

5 messages · Page 1 of 1 (latest)

opal tulip
#

Playground link

I’ve been beating my head against a wall trying to get the returned type of a callback to propagate through a recursively called generic function. I’m trying to chain callbacks together to act on objects with similar types but some differing methods and properties, so getting the typing going is a must as this is going in a library. The code itself runs exactly as intended when the types are stripped out, so I know I’m on the right track.

#

Using TS v5.9.3 but the behaviour appears the same in v6.0.2

#

I have tried a few different approaches but all end up with more or less the same behaviour in the end, the error I’ve seen most frequently is “X could be instantiated with a different subtype of Y”

thick badger
#

Here is one way to do it:

pulsar flameBOT
#
nonspicyburrito#0

Preview:```ts
type NextAction<T> = {
act: <U>(
cb: (p: T) => U | Promise<U>
) => NextAction<U>
finish: () => Promise<T>
}

const nextAction = <T>(
actions: ((arg: unknown) => unknown)[],
next: (arg: unknown) => T | Promise<T>
): NextAction<T> => ({
act: cb =>
nextAction([...act
...```