Skip to content
The Chat Archive
qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K

In typescript I refer to type: structuredOutput?:…

  1. User

    In typescript I refer to type: structuredOutput?: BaseModelParams['generationConfig']['responseSchema'] The problem is that inside BaseModel the generationConfig is optional:

    export declare interface BaseModelParams {
        generationConfig?: GenerationConfig;
    

    so I cannot refer to responseSchema How can I fix type to be able to refer to responseSchema?

  2. Assistant

    You're encountering this issue because you're trying to access a nested property (responseSchema) inside an optional property (generationConfig) of an interface (BaseModelParams). TypeScript prevents you from directly accessing nested properties of optional fields without first checking that the parent field exists.

    Problem:

    structuredOutput?: BaseModelParams['generationConfig']['responseSchema']
    

    Since generationConfig is optional, accessing generationConfig['responseSchema'] directly could result in reading a property of undefined, which is unsafe and results in a type error.


    ✅ Solution: Use a nested ? index access or conditional type

    You have a few clean and safe options to fix this.


    ✅ Option 1: Use nested optional indexing

    TypeScript allows optional property access with ? in indexed access types:

    structuredOutput?: BaseModelParams['generationConfig'] extends undefined
      ? never
      : Exclude<BaseModelParams['generationConfig'], undefined>['responseSchema'];
    

    Or more cleanly using optional chaining in indexed access:

    structuredOutput?: BaseModelParams['generationConfig']?['responseSchema'];
    

    This is TypeScript 4.6+ feature and is the cleanest and most idiomatic solution.

    ✅ Explanation:

    BaseModelParams['generationConfig']?['responseSchema']
    
    • BaseModelParams['generationConfig'] gets the type of generationConfig, which is GenerationConfig | undefined.
    • Adding ?['responseSchema'] means: "if the left-hand side is null or undefined, return undefined", otherwise get the type of responseSchema.

    So the resulting type is effectively:

    GenerationConfig['responseSchema'] | undefined
    

    Which is exactly what you want.


    ✅ Option 2: Use a helper type

    If you're going to do this kind of access often, define a helper type:

    type OptionalProp<T, K1 extends keyof any, K2 extends keyof any> =
      T extends { [P in K1]: { [P2 in K2]?: infer V } }
        ? V
        : undefined;
    

    Then:

    structuredOutput?: OptionalProp<BaseModelParams, 'generationConfig', 'responseSchema'>;
    

    This is more verbose but can be useful in complex scenarios.


    ✅ Option 3: Use a conditional type directly

    structuredOutput?: BaseModelParams['generationConfig'] extends infer G
      ? G extends { responseSchema?: infer R } ? R : undefined
      : undefined;
    

    This is more flexible if you want to do deeper nesting or conditional logic.


    ✅ Summary

    The best and simplest solution is:

    structuredOutput?: BaseModelParams['generationConfig']?['responseSchema'];
    

    This safely and concisely references a nested property inside an optional field.

    Let me know if you also want to enforce that responseSchema must exist if generationConfig exists!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.