I am looking to incorporate the following feature:
const foo = <S>(newState: S | ((prevState: S) => S)) => {...}
This function should accept either a new state of type S
, or a function that generates the new state from the old state.
In my implementation, how can I distinguish between these two options? I have considered using typeof newState === 'function'
, but I realize that S
could also be a function in this case.
Although I am working with TypeScript, I believe the solution may be more related to JavaScript than TypeScript itself.