createIsomorphicFn
- Creating different implementations for the same function based on environment
#2937
Closed
EskiMojo14
started this conversation in
Ideas
Replies: 1 comment
-
released in https://github.com/TanStack/router/releases/tag/v1.87.0 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Copying over a feature idea discussed in a Discord thread.
This would be a new API similar to
serverOnly
but allowing for a client implementation to be provided too.Basic example:
Each implementation should accept the same arguments, however can return a different result - the result type will be a union of the possible results.
Each implementation is optional, if not provided it should default to a no-op (and its result type would be
undefined
).For example a server only function could look like
I've created a Typescript playground (alternate approach) for how this could be achieved type-wise.
The transformer for this should be fairly simple, just extracting the correct implementation for the environment (or supplying a no-op if none is provided).
e.g.
The transformer might also want to warn if no implementations are provided (i.e.
const fn = createIsomorphicFn()
) as this seems like a mistake - it could still provide a no-op to avoid runtime errors.Beta Was this translation helpful? Give feedback.
All reactions