In typescript I refer to type: structuredOutput?:…
- 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?
- 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
generationConfigis optional, accessinggenerationConfig['responseSchema']directly could result in reading a property ofundefined, which is unsafe and results in a type error.
✅ Solution: Use a nested
?index access or conditional typeYou 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 ofgenerationConfig, which isGenerationConfig | undefined.- Adding
?['responseSchema']means: "if the left-hand side isnullorundefined, returnundefined", otherwise get the type ofresponseSchema.
So the resulting type is effectively:
GenerationConfig['responseSchema'] | undefinedWhich 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
responseSchemamust exist ifgenerationConfigexists!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.