Skip to content

Commit 4d75ba6

Browse files
fix: force both implementations
1 parent 5cc2c13 commit 4d75ba6

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

packages/start-client-core/src/createIsomorphicFn.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ export interface ClientOnlyFnWithType<TArgs extends Array<any>, TReturnType>
3636
) => IsomorphicFn<TArgs, TReturnType, TReturnType>
3737
}
3838

39-
export interface IsomorphicFnWithType<TArgs extends Array<any>, TReturnType>
40-
extends IsomorphicFn<TArgs, TReturnType> {
41-
server: (
42-
serverImpl: (...args: TArgs) => TReturnType,
43-
) => ServerOnlyFnWithType<TArgs, TReturnType>
44-
client: (
45-
clientImpl: (...args: TArgs) => TReturnType,
46-
) => ClientOnlyFnWithType<TArgs, TReturnType>
39+
export interface IsomorphicFnWithType<TArgs extends Array<any>, TReturnType> {
40+
server: (serverImpl: (...args: TArgs) => TReturnType) => {
41+
client: (
42+
clientImpl: (...args: TArgs) => TReturnType,
43+
) => IsomorphicFn<TArgs, TReturnType, TReturnType>
44+
}
45+
client: (clientImpl: (...args: TArgs) => TReturnType) => {
46+
server: (
47+
serverImpl: (...args: TArgs) => TReturnType,
48+
) => IsomorphicFn<TArgs, TReturnType, TReturnType>
49+
}
4750
}
4851

4952
export interface IsomorphicFnBase extends IsomorphicFn {
@@ -63,10 +66,12 @@ export interface IsomorphicFnBase extends IsomorphicFn {
6366
// if we use `createIsomorphicFn` in this library itself, vite tries to execute it before the transformer runs
6467
// therefore we must return a dummy function that allows calling `server` and `client` method chains.
6568
export function createIsomorphicFn(): IsomorphicFnBase {
66-
return {
67-
$withType: () => {},
69+
const baseFns = {
6870
server: () => ({ client: () => () => {} }),
6971
client: () => ({ server: () => () => {} }),
72+
}
73+
return {
74+
$withType: () => baseFns,
75+
...baseFns,
7076
} as any
7177
}
72-

packages/start-client-core/src/tests/createIsomorphicFn.test-d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ test('createIsomorphicFn with type', () => {
100100
expectTypeOf(fn2).toBeCallableWith('foo', 1)
101101
expectTypeOf(fn2).returns.toEqualTypeOf<boolean>()
102102

103+
const noServerImpl = createIsomorphicFn()
104+
.$withType<(a: string) => string>()
105+
.client((a) => 'data')
106+
expectTypeOf(noServerImpl).not.toBeFunction()
107+
108+
// Missing server implementation
109+
const noClientImpl = createIsomorphicFn()
110+
.$withType<(a: string) => string>()
111+
.server((a) => 'data')
112+
expectTypeOf(noClientImpl).not.toBeFunction()
113+
103114
const _invalidFnReturn = createIsomorphicFn()
104115
.$withType<(a: string) => string>()
105116
.server(() => '1')

0 commit comments

Comments
 (0)