-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Description
When using useSession and wrapping it in a function (common pattern for creating a typed session helper), TypeScript cannot infer the return type without a reference to internal module paths.
Expected Behavior
The SessionManager type should be exported from @tanstack/react-start/server (and equivalent for solid/vue) so users can properly type their session helper functions.
Actual Behavior
Only SessionConfig is exported from the public API. SessionManager is defined in session.ts but not re-exported through index.ts.
This causes the following TypeScript error:
The inferred type of 'getAppSession' cannot be named without a reference to
'../../../../../../node_modules/.pnpm/@[email protected]/node_modules/@tanstack/start-server-core/dist/esm/session'.
This is likely not portable. A type annotation is necessary.
Reproduction
// utils/session.ts
import { useSession } from '@tanstack/react-start/server'
type SessionData = {
userId?: string
}
// ❌ This causes type error - cannot infer return type
export function useAppSession() {
return useSession<SessionData>({
password: process.env.SESSION_SECRET!,
})
}Workaround
Currently users must derive the type manually:
import { useSession } from '@tanstack/react-start/server'
type SessionData = { userId?: string }
// Derive SessionManager type since it's not exported
type AppSessionManager = Awaited<ReturnType<typeof useSession<SessionData>>>
export function useAppSession(): Promise<AppSessionManager> {
return useSession<SessionData>({
password: process.env.SESSION_SECRET!,
})
}Suggested Fix
Export SessionManager, Session, SessionData, and SessionUpdate types from @tanstack/start-server-core/src/index.tsx:
// packages/start-server-core/src/index.tsx
- export type { SessionConfig } from './session'
+ export type { SessionConfig, SessionManager, Session, SessionData, SessionUpdate } from './session'These types are already defined and used internally in request-response.ts, they just need to be re-exported publicly.
Environment
@tanstack/react-start: 1.149.4 / 1.150.0@tanstack/start-server-core: 1.150.0- TypeScript: 5.x
- Node: 22.x
Additional Context
The official documentation shows the useAppSession() pattern but doesn't mention this typing issue:
The types exist in packages/start-server-core/src/session.ts:
SessionManager<T>(lines 42-47)Session<T>(lines 33-37)SessionData<T>(line 32)SessionUpdate<T>(lines 39-40)