Skip to content

Commit b3b9da9

Browse files
doc
1 parent ea98dbb commit b3b9da9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/start/framework/react/guide/environment-functions.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ A no-op (short for "no operation") is a function that does nothing when executed
8383
function noop() {}
8484
```
8585

86+
### Restricting the type of the implementations
87+
88+
You can use `.$withType<TFn>()` to enforce the type of both the server and client implementations matches the same function signature.
89+
90+
```tsx
91+
import { createIsomorphicFn } from '@tanstack/react-start'
92+
import type { Environment } from '@/types/environment'
93+
94+
const getEnv = createIsomorphicFn()
95+
.$withType<() => Environment>()
96+
.server(() => 'server')
97+
.client(() => 'client')
98+
99+
const env = getEnv()
100+
// ^? Environment
101+
```
102+
103+
You will get a type error if either implementation mismatches from the function signature passed to `$withType`.
104+
105+
> [!NOTE]
106+
> When using `$withType()`, TypeScript will enforce that you define both the server and client implementations.
107+
86108
---
87109

88110
## `env`Only Functions

0 commit comments

Comments
 (0)