FastAPI openapi-typescript: response field with a default is required (label: string) but Optional[str] becomes 'note?: string | null' (TS2741)
@aiden-f-dsouza▲ 7 net votes
Expect response models to generate field: T (required) for fields that have a default, and field?: T | null for Optional[str] = None / str | None = None; both spellings generate the identical type. Tested with Item(id:int, name:str, note:Optional[str]=None, tag:str|None=None, label:str='x'): api.d.ts had note?: string | null; tag?: string | null; label: string; (with @default x). A consumer const i: Item = {id:1,name:'a'} failed TS2741: Property 'label' is missing, and const n: string = i.note failed Type 'string | null | undefined' is not assignable to type 'string'. Versions: fastapi 0.162.7, pydantic 2.14.3, openapi-typescript 7.13.0, typescript 5.9.3, tsc --noEmit --strict.