Type Inference Utilities
The SDK exports three main type inference utilities:InferRow
Extract row types from datasources
InferParams
Extract parameter types from pipes
InferOutputRow
Extract output types from pipes
InferRow
Extract the row type from a datasource definition for type-safe data ingestion:~/workspace/source/src/infer/index.ts:42-60
Using Inferred Types
Use the inferred type for type-safe data ingestion:Type Mapping
The SDK maps ClickHouse types to TypeScript types:| ClickHouse Type | TypeScript Type |
|---|---|
String, FixedString, UUID | string |
Int8, Int16, Int32, UInt8, UInt16, UInt32, Float32, Float64 | number |
Int64, UInt64, Int128, UInt128, Int256, UInt256 | bigint |
Bool | boolean |
Date, DateTime, DateTime64 | string |
Array(T) | T[] |
Map(K, V) | Map<K, V> |
Tuple(T1, T2, ...) | [T1, T2, ...] |
JSON | unknown (customizable) |
Nullable(T) | T | null |
Nullable Types
Nullable columns are automatically typed as union withnull:
InferParams
Extract parameter types from pipe definitions:~/workspace/source/src/infer/index.ts:88-111
Using Parameter Types
Use inferred parameter types for type-safe queries:Optional vs Required Parameters
The SDK correctly distinguishes between optional and required parameters:~/workspace/source/src/infer/index.ts:63-83
InferOutputRow
Extract the output row type from pipe definitions:~/workspace/source/src/infer/index.ts:137-142
Using Output Types
Use the inferred type for type-safe result handling:InferOutput (Array Type)
For cases where you need the full array type:~/workspace/source/src/infer/index.ts:128-135
Advanced Type Inference
Custom Branded Types
For stricter type safety, use branded types:Partial Types
For partial updates, usePartialRow:
~/workspace/source/src/infer/index.ts:168-172
Extract Schema Type
Extract the raw schema definition:~/workspace/source/src/infer/index.ts:174-177
Materialized View Target Types
Extract the target datasource from a materialized view:~/workspace/source/src/infer/index.ts:194-235
Type Inference Helper
For individual type validators:~/workspace/source/src/infer/index.ts:14-28
Best Practices
Export types alongside definitions
Export types alongside definitions
Export inferred types alongside your datasource and pipe definitions for easy reuse:
Create a central types file
Create a central types file
Consider creating a central types file that re-exports all your inferred types:
Use in API routes
Use in API routes
Use inferred types in your API routes for end-to-end type safety:
Related Resources
Datasources
Learn about datasource schema definitions
Pipes
Learn about pipe parameter and output definitions
Type Validators
Complete reference of t.* validators
Parameter Validators
Complete reference of p.* validators