Splitting TypeScript Config in TanStack Start
Learn how to implement Cloudflare's Cache API in TanStack Start by splitting TypeScript configurations to resolve type conflicts and ESLint parsing errors.
Contents100%
When building full-stack applications with frameworks like TanStack Start, we often find ourselves in a “type identity crisis.”
On one hand, our client-side code needs access to the DOM (window, document, localStorage). On the other, our server functions—running on Cloudflare Workers—need access to Worker-specific APIs (KV, D1, and the Cache API).
The problem? If you put everything in one tsconfig.json, the combined globals can make browser APIs appear valid in server code and Worker APIs appear valid in browser code. Your code can then crash at runtime despite passing the type check.
Recently, while implementing the Cloudflare Cache API to optimize authentication checks, I hit a wall of configuration errors. Here is how I solved them.
The Problem: The “Everything” Config
Initially, my project had a single tsconfig.json. As soon as I tried to use caches.default inside a .server.ts file, I faced three main issues:
- Type Clashes: The global types for the browser and Cloudflare Workers often overlap or conflict.
- Path Alias Loss: When splitting configs to solve the type clash, my path aliases (like
#/utils/*) stopped working in server files. - ESLint Meltdown: ESLint started throwing parsing errors, claiming that server files were not included in the project’s TSConfig.
To fix this, I needed a Solution-Style TSConfig architecture.
The Solution: A Layered Configuration
The goal was to create separate client and server type-checking surfaces while sharing common settings. I moved the configuration into a dedicated /tsconfig folder to keep the root directory clean. This is not, by itself, a strict runtime boundary.
1. The Base Layer (tsconfig.base.json)
First, I created a base configuration. This is the “single source of truth” for shared type-check settings like strictness, target version, and—most importantly—path aliases. Relative paths declared here are resolved from this file, so ../src/* points to the project’s src directory. TypeScript paths only affects compile-time lookup; Vite also needs equivalent alias resolution.
// tsconfig/tsconfig.base.json
{
"compilerOptions": {
"target": "ES2022",
"moduleResolution": "bundler",
"paths": {
"#/*": ["../src/*"],
"@/*": ["../src/*"]
},
"strict": true,
"noEmit": true
// ... other shared settings
}
}2. The Specialization Layer
Next, I created two separate configs that extend the base.
The Server Config: This includes only the server files and the Cloudflare type definitions. I used our local worker-configuration.d.ts to provide the global caches object and environment variables.
// tsconfig/tsconfig.server.json
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"lib": ["ES2022"],
"types": []
},
"include": ["../**/*.server.ts", "../**/*.server.tsx", "../worker-configuration.d.ts"]
}The Client Config: This includes the DOM libraries and excludes all .server.ts files to prevent accidental leaks.
// tsconfig/tsconfig.client.json
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"types": ["vite/client"]
},
"include": ["../**/*.ts", "../**/*.tsx", "../eslint.config.js", "../prettier.config.js", "../vite.config.js"],
"exclude": ["../**/*.server.ts", "../**/*.server.tsx"]
}3. The Solution Layer (tsconfig.json at root)
Finally, I kept a tsconfig.json at the root. This file doesn’t contain rules; it acts as a solution-style coordinator for editor tooling using Project References. If you also intend to build with tsc -b, configure the leaf projects to satisfy Project References requirements, including composite and the appropriate declaration/output strategy; references alone do not complete the build configuration.
{
"files": [],
"references": [{ "path": "./tsconfig/tsconfig.server.json" }, { "path": "./tsconfig/tsconfig.client.json" }]
}Fixing the ESLint “Parsing Error”
Even with the TSConfigs fixed, ESLint was still complaining:
“Parsing error: ESLint was configured to run on … however, that TSConfig does not include this file.”
This error comes from @typescript-eslint/parser during typed linting. Its older project option requires the linted files to belong to the supplied TSConfigs and does not automatically resolve every file through Project References.
A fix is to use the TypeScript ESLint Project Service, which uses TypeScript’s project APIs to find the appropriate configuration. Files that still belong to no project—such as some root JavaScript config files—may need a narrowly scoped allowDefaultProject configuration.
// eslint.config.js
export default [
{
languageOptions: {
parserOptions: {
project: false, // Disable the old 'project' setting
projectService: true, // Let TS decide the config per file
tsconfigRootDir: import.meta.dirname,
},
},
},
];Implementing the Cache API (The Right Way)
With the configuration out of the way, using the Cloudflare Cache API became seamless.
When I first encountered the caches error, it was tempting to simply use (caches as any).default to silence the compiler. However, using any in a project meant to be type-safe is a slippery slope.
Instead, I relied on the worker-configuration.d.ts file. It’s worth emphasizing that We do not edit this file manually. It is generated automatically whenever you run the wrangler types command.
The CacheStorage type was already generated inside it. The server project includes that declaration file while omitting DOM libraries, so its global API surface comes from the generated Workers declarations rather than a priority contest with lib.dom.d.ts. Regenerate the types after relevant Wrangler configuration changes.
With this setup, I got full autocomplete and type safety without a single type cast. Now, in auth.server.ts, the code is clean and robust:
export async function authenticate(cookie: string) {
const cache = caches.default; // Fully typed!
const cacheKey = new Request(`https://lafyeri.internal/auth/${hash(cookie)}`);
const cachedResponse = await cache.match(cacheKey);
if (cachedResponse) return await cachedResponse.json();
// ... fetch from API and cache the result
}For Libraries like shadcn/ui
If you have components.json like this one:
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "radix-mira",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "",
"css": "src/styles.css",
"baseColor": "stone",
"cssVariables": true,
"prefix": ""
},
"iconLibrary": "lucide",
"rtl": false,
"aliases": {
"components": "#/components",
"utils": "#/lib/utils",
"ui": "#/components/ui",
"lib": "#/lib",
"hooks": "#/hooks"
},
"menuColor": "default",
"menuAccent": "subtle",
"registries": {}
}The aliases in components.json tell shadcn where to generate components and how to rewrite imports. In a paths-based setup, back them with compilerOptions.paths so #/components resolves to src/components. Shadcn also supports package.json#imports with bundler package-import resolution, so a root TSConfig paths field is not the only possible mechanism.
{
"compilerOptions": {
"paths": {
"#/*": ["./src/*"],
"@/*": ["./src/*"]
}
},
"files": [],
"references": [{ "path": "./tsconfig/tsconfig.server.json" }, { "path": "./tsconfig/tsconfig.client.json" }]
}The Runtime vs. Build-time Divide
One final nuance when dealing with a TanStack Start project on Cloudflare is managing your environment variables. You are generally dealing with two entirely different environment systems:
- The Runtime Env: These are the secrets, D1 databases, and KV namespaces accessed via
import { env } from 'cloudflare:workers'. They are available in deployed Workers and supported local Workers development, where resources are usually simulated locally.wrangler typesgenerates matching declarations. - The Build-time Env: Variables prefixed with
VITE_are exposed to client code throughimport.meta.envand statically replaced in production builds. They are public, so never put secrets in them.
Vite supplies the base import.meta.env types; augment ImportMetaEnv when you want property-specific IntelliSense or stricter checking. This behavior does not depend on the layered TSConfig setup.
Conclusion: Lessons Learned
Moving to a split configuration might seem like overkill at first, but it provides three critical benefits:
- Earlier Type Feedback: Files checked solely by the server config do not receive DOM globals, reducing accidental use of
windowanddocument. This is not a strict runtime boundary: imported files can still enter a program despiteexclude, and TanStack Start’s import protection separately checks client/server imports. - Potential IDE and Build Benefits: Project References can reduce type-checking work, although the amount loaded depends on the project graph and editor settings; VS Code is not guaranteed to load only one configuration.
- Architectural Clarity: The
/tsconfigfolder separates the “how it’s built” (config) from the “what it does” (src).
When your project grows beyond a simple SPA, don’t be afraid to split your configs. A little bit of setup time saves hours of debugging “Cannot find module” or “Type mismatch” errors down the road.