Skip to main content

TypeScript with Solid Query

Solid Query is written in TypeScript and provides comprehensive type safety out of the box. This guide covers how to get the most out of TypeScript with Solid Query.

Type Inference

Solid Query automatically infers types from your query and mutation functions:

Generic Type Parameters

You can explicitly specify type parameters for more control:

Type Parameters Explained

  • TQueryFnData: The type returned by the queryFn
  • TError: The type of errors that can be thrown (defaults to Error)
  • TData: The type of data in the query result (useful with select)
  • TQueryKey: The type of the query key (for type-safe query keys)

Query Options Type Safety

Use the queryOptions helper for type-safe, reusable query configurations:
Use as const on query keys to preserve literal types for better type inference.

Mutation Type Safety

Mutations also support full type inference:

Infinite Query Types

Type-safe infinite queries with proper page param typing:

Custom Query Client Types

Extend the QueryClient with custom configuration types:

Type-Safe Query Keys

Create a type-safe query key factory:

Data Transformation with Select

Use the select option to transform query data with full type safety:

Accessor Types

Solid Query uses SolidJS Accessors for reactive options:

Type Utilities

Solid Query exports useful type utilities:

Initial Data with Types

Error Handling Types

Best Practices

Follow these TypeScript best practices with Solid Query:
  1. Define Interfaces First: Always define your data interfaces before using them in queries
  2. Use queryOptions: Leverage the queryOptions helper for reusable, type-safe configurations
  3. Explicit Return Types: Add explicit return types to your query and mutation functions
  4. Use as const: Use as const for query keys to preserve literal types
  5. Type Guards: Implement type guards for runtime type checking when needed
  6. Generic Helpers: Create generic helper functions for common query patterns

Version Support

Solid Query is tested against TypeScript versions:
  • TypeScript 5.0+
  • TypeScript 5.1+
  • TypeScript 5.2+
  • TypeScript 5.3+
  • TypeScript 5.4+
  • TypeScript 5.5+
  • TypeScript 5.6+
  • TypeScript 5.7+ (latest)
For the best experience, use TypeScript 5.4 or later.

Next Steps

Quick Start

Build your first type-safe Solid Query app

DevTools

Debug your queries with DevTools