Skip to content
XenonDocs
Esc
    GitHub
    Docs

    Type inference

    Generate TypeScript types directly from collection definitions without code generation steps.

    On this page

    Automatic typing

    You can derive TypeScript types directly from collection schemas without running code generators.

    src/xenon.config.ts
    import { defineCollection, text, textarea, number } from "xenon-cms";
    import type {
      InferCollectionType,
      InferCollectionData,
      InferCreateInput,
    } from "xenon-cms";
    
    export const posts = defineCollection({
      slug: "posts",
      fields: {
        title: text({ required: true }),
        excerpt: textarea(),
        views: number({ defaultValue: 0 }),
      },
    });
    
    // Full document including system attributes
    export type Post = InferCollectionType<typeof posts>;
    
    // Declared field values only
    export type PostData = InferCollectionData<typeof posts>;
    
    // Input accepted for creating new documents
    export type CreatePostInput = InferCreateInput<typeof posts>;

    Xenon field helpers use const generic parameters to infer literal types and required constraints automatically.

    Type utilities

    Xenon exports several helper types:

    TypeTargetDescription
    InferCollectionType<C>CollectionFull document representation: declared fields plus system keys (id, slug, status, createdAt, updatedAt).
    InferCollectionData<C>CollectionDeclared fields and upload metadata only, excluding system keys.
    InferCreateInput<C>CollectionShape required when creating a document via xenon.create.
    InferUpdateInput<C>CollectionShape accepted when updating a document (Partial<InferCreateInput<C>>).
    InferDraftInput<C>CollectionRelaxed shape for draft saves where required fields may be omitted or null.
    CollectionSlug<Config>ConfigUnion of collection slug strings defined in the config.
    InferConfigDocuments<Config>ConfigObject map of all collections keyed by slug to their InferCollectionType.

    Field inference rules

    Xenon computes read and write types based on the required and defaultValue configuration of each field:

    Field declarationRead type (InferCollectionData)Create input (InferCreateInput)
    required: true, no defaultkey: Vkey: V (required)
    required: true, with defaultkey: Vkey?: V (optional)
    not required, with defaultkey: Vkey?: V | null (optional)
    not required, no defaultkey?: V | nullkey?: V | null (optional)

    Fields with a defaultValue guarantee a value on read (key: V), because Xenon replaces null or undefined values with the default during normalization.

    Next steps