Tighten TypeScript safety: strict flags, typed linting, public-API type tests #564
Context#
TanStack AI's primary value proposition is type safety, but a configuration audit shows several high-value strict flags off and no typed-linting rules active. The compiler baseline is already strong (strict: true, noUncheckedIndexedAccess: true, noUnusedLocals, noUnusedParameters, noImplicitReturns, isolatedModules, checkJs), but the codebase currently contains ~191 : any, ~25 as any, ~43 as unknown as, and 9 @ts-expect-error/@ts-ignore in non-test source — a notable surface for a library that sells type safety.
Scope (single PR)#
Compiler flags (root tsconfig.json)#
-
exactOptionalPropertyTypes: true— distinguishesfoo?: Tfromfoo: T | undefined. Dominant source of churn; expect cascades throughModelMessage, provider options, and tool schemas. -
noImplicitOverride: true -
noPropertyAccessFromIndexSignature: true -
noFallthroughCasesInSwitch: true -
useDefineForClassFields: true
Explicitly deferred: verbatimModuleSyntax — @typescript-eslint/consistent-type-imports already enforces import discipline; flipping verbatim would force export-statement churn for low marginal value.
Typed ESLint rules (new tanstack/ai/typed block in root eslint.config.js)#
Scoped to packages/typescript/**/src/**/*.{ts,tsx} (skip tests, dist, examples), using parserOptions.projectService: true.
-
@typescript-eslint/no-floating-promises— error -
@typescript-eslint/no-misused-promises— error -
@typescript-eslint/await-thenable— error -
@typescript-eslint/switch-exhaustiveness-check— error -
@typescript-eslint/no-explicit-any— warn (baseline; ratchet later) -
@typescript-eslint/no-non-null-assertion— warn -
@typescript-eslint/consistent-type-exports— error -
@typescript-eslint/prefer-readonly— warn
Why these specifically: streaming + agent-loop code (packages/typescript/ai/src/core/chat.ts) is async-heavy, so floating-promise + misused-promise bugs are the highest-leverage class to catch. Stream chunks, content parts, and tool states are discriminated unions where switch-exhaustiveness-check turns "added a new variant, forgot a case" into a compile error.
Suppression discipline#
- Override
@typescript-eslint/ban-ts-commentto require descriptions on@ts-expect-erroras well ({ 'ts-expect-error': 'allow-with-description', 'ts-ignore': false, 'ts-nocheck': true }). - Add CI step (Nx target or workspace script) that fails if
grep -rE "@ts-(ignore|nocheck)\b" packages/typescript/*/srcreturns anything.
Public-API type-test coverage#
Following the existing pattern in packages/typescript/ai/tests/image-per-model-type-safety.test.ts and packages/typescript/ai-openai/tests/tools-per-model-type-safety.test.ts:
-
chat-per-model-type-safety.test.tsin each ofai,ai-anthropic,ai-gemini,ai-openai(provider options × model coverage). -
use-chat-types.test.tsinai-solid— parity withai-react,ai-vue,ai-svelte. -
InferChatMessagesround-trip test inai-client/tests/, proving tool input/output types propagate to message-array types.
Fix fallout#
- Resolve every compile error surfaced by the new compiler flags.
- Resolve every
error-level lint surfaced by the new typed rules. Treat eachno-floating-promisessite individually rather than blanket-suppressing — these often indicate real concurrency bugs in streaming/tool-call code.
Out of scope (deliberate)#
verbatimModuleSyntax— see above.- Migrating to
tsd/expect-typefor type-tests — the existing@ts-expect-errorpattern is sufficient. - Sweeping removal of
as unknown ascasts — many are load-bearing vendor-SDK bridges; address case-by-case asno-explicit-anywarnings surface them.
Verification#
pnpm test:types
pnpm test:eslint
pnpm test:lib
pnpm --filter @tanstack/ai-e2e test:e2e
pnpm test:build
And:
grep -rE "@ts-(ignore|nocheck)\b" packages/typescript/*/src
should return no matches.
Risk#
Large coordinated diff spanning every workspace package. exactOptionalPropertyTypes is the dominant source of compile-time changes — reviewers should focus on whether each adjusted optional-property site is semantically correct (absent vs explicit undefined). no-floating-promises fixes can subtly change concurrency ordering in stream consumers and agent loops; review each site individually.
AlemTuzlak closed this
Sign in to join this conversation.