[{"data":1,"prerenderedAt":606},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Finferred-types":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":605,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Finferred-types.md",{},"Infer TypeScript types from Zod schemas",{"type":8,"value":9,"toc":596},"minimark",[10,35,40,61,234,242,246,260,285,310,334,396,404,408,417,488,508,512,540],[11,12,13,14,18,19,22,23,26,27,30,31,34],"p",{},"Set ",[15,16,17],"code",{},"inferred"," to export a ",[15,20,21],{},"z.infer"," type next to each Zod schema, or ",[15,24,25],{},"z.input"," and ",[15,28,29],{},"z.output"," types for codecs, so you can skip ",[15,32,33],{},"@kubb\u002Fplugin-ts",".",[36,37,39],"h2",{"id":38},"turn-on-inferred-types","Turn on inferred types",[11,41,42,43,46,47,49,50,53,54,57,58,34],{},"With ",[15,44,45],{},"inferred: true",", each schema gets a ",[15,48,21],{}," type named after it: the PascalCase schema name plus ",[15,51,52],{},"SchemaType",", so ",[15,55,56],{},"petSchema"," gets ",[15,59,60],{},"PetSchemaType",[62,63,64,124],"code-group",{},[65,66,71],"pre",{"language":67,"filename":68,"code":69,"class":70},"typescript","kubb.config.ts","import { pluginZod } from '@kubb\u002Fplugin-zod'\n\npluginZod({ inferred: true })","highlights",[15,72,74,101,102,101,104],{"class":73},"language-typescript",[75,76,79,83,87,91,94,97],"span",{"class":77,"style":78},"line","display: inline",[75,80,82],{"style":81},"color:#d73a49;--hls-dark:#f97583","import ",[75,84,86],{"style":85},"color:#005cc5;--hls-dark:#79b8ff","{ ",[75,88,90],{"style":89},"color:#24292e;--hls-dark:#e1e4e8","pluginZod ",[75,92,93],{"style":85},"} ",[75,95,96],{"style":81},"from ",[75,98,100],{"style":99},"color:#032f62;--hls-dark:#9ecbff","'@kubb\u002Fplugin-zod'","\n",[75,103],{"class":77,"style":78},[75,105,106,110,113,115,118,121],{"class":77,"style":78},[75,107,109],{"style":108},"color:#6f42c1;--hls-dark:#b392f0","pluginZod",[75,111,112],{"style":85},"({ ",[75,114,17],{"style":85},[75,116,117],{"style":81},": ",[75,119,120],{"style":85},"true ",[75,122,123],{"style":85},"})",[65,125,128],{"language":67,"filename":126,"code":127,"class":70},"src\u002Fgen\u002Fzod\u002FpetSchema.ts","export const petSchema = z.object({\n  id: z.int(),\n  name: z.string(),\n})\n\nexport type PetSchemaType = z.infer\u003Ctypeof petSchema>",[15,129,130,101,155,101,178,101,198,101,202,101,204],{"class":73},[75,131,132,135,138,141,144,147,149,152],{"class":77,"style":78},[75,133,134],{"style":81},"export ",[75,136,137],{"style":81},"const ",[75,139,140],{"style":89},"petSchema ",[75,142,143],{"style":81},"= ",[75,145,146],{"style":89},"z",[75,148,34],{"style":81},[75,150,151],{"style":108},"object",[75,153,154],{"style":85},"({",[75,156,157,160,163,165,167,169,172,175],{"class":77,"style":78},[75,158,159],{"style":85},"  ",[75,161,162],{"style":85},"id",[75,164,117],{"style":81},[75,166,146],{"style":89},[75,168,34],{"style":81},[75,170,171],{"style":108},"int",[75,173,174],{"style":85},"()",[75,176,177],{"style":81},",",[75,179,180,182,185,187,189,191,194,196],{"class":77,"style":78},[75,181,159],{"style":81},[75,183,184],{"style":85},"name",[75,186,117],{"style":81},[75,188,146],{"style":89},[75,190,34],{"style":81},[75,192,193],{"style":108},"string",[75,195,174],{"style":85},[75,197,177],{"style":81},[75,199,200],{"class":77,"style":78},[75,201,123],{"style":85},[75,203],{"class":77,"style":78},[75,205,206,208,211,214,216,218,220,223,226,229,231],{"class":77,"style":78},[75,207,134],{"style":81},[75,209,210],{"style":81},"type ",[75,212,213],{"style":108},"PetSchemaType ",[75,215,143],{"style":81},[75,217,146],{"style":89},[75,219,34],{"style":81},[75,221,222],{"style":85},"infer",[75,224,225],{"style":81},"\u003C",[75,227,228],{"style":81},"typeof ",[75,230,56],{"style":89},[75,232,233],{"style":81},">",[11,235,236,237,34],{},"To rename the types, see ",[238,239,241],"a",{"href":240},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Fcustomization#prefix-inferred-types","Prefix inferred types",[36,243,245],{"id":244},"type-codecs-on-each-side","Type codecs on each side",[11,247,42,248,251,252,255,256,259],{},[15,249,250],{},"inferred: 'codec'",", Kubb writes ",[15,253,254],{},"z.input\u003Ctypeof schema>"," for request schemas (bodies and params) and ",[15,257,258],{},"z.output\u003Ctypeof schema>"," for response schemas.",[261,262,263,271,276],"ul",{},[264,265,266,267,270],"li",{},"Use it with codecs: a request takes the domain value, such as a ",[15,268,269],{},"Date",", which the schema encodes, and a response returns the decoded value.",[264,272,273,274,34],{},"Clients and query hooks then take their operation types from these types instead of ",[15,275,33],{},[264,277,278,281,282,34],{},[15,279,280],{},"'direction'"," is the deprecated name for ",[15,283,284],{},"'codec'",[286,287,42,289,291,292,295,296,302,303,309],"blockquote",{"as":288},"warning",[15,290,284],{},", set ",[15,293,294],{},"validator: { request: 'zod', params: 'zod' }"," in ",[238,297,299],{"href":298},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch\u002Foptions#validator",[15,300,301],{},"@kubb\u002Fplugin-fetch"," or ",[238,304,306],{"href":305},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Foptions#validator",[15,307,308],{},"@kubb\u002Fplugin-axios",". Without request validation, the client sends the domain value unencoded.",[11,311,312,313,317,318,321,322,325,326,329,330,333],{},"When a ",[238,314,316],{"href":315},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Foptions#printer","printer"," handler returns a different expression per ",[15,319,320],{},"this.options.direction",", Kubb also writes an ",[15,323,324],{},"${name}InputSchema",", which request bodies use, also through a ",[15,327,328],{},"$ref",". Its type gets the ",[15,331,332],{},"InputSchemaType"," suffix:",[65,335,338],{"language":67,"filename":336,"code":337,"class":70},"src\u002Fgen\u002Fzod\u002FslotSchema.ts","export type SlotSchemaType = z.output\u003Ctypeof slotSchema>\n\nexport type SlotInputSchemaType = z.input\u003Ctypeof slotInputSchema>",[15,339,340,101,367,101,369],{"class":73},[75,341,342,344,346,349,351,353,355,358,360,362,365],{"class":77,"style":78},[75,343,134],{"style":81},[75,345,210],{"style":81},[75,347,348],{"style":108},"SlotSchemaType ",[75,350,143],{"style":81},[75,352,146],{"style":89},[75,354,34],{"style":81},[75,356,357],{"style":85},"output",[75,359,225],{"style":81},[75,361,228],{"style":81},[75,363,364],{"style":89},"slotSchema",[75,366,233],{"style":81},[75,368],{"class":77,"style":78},[75,370,371,373,375,378,380,382,384,387,389,391,394],{"class":77,"style":78},[75,372,134],{"style":81},[75,374,210],{"style":81},[75,376,377],{"style":108},"SlotInputSchemaType ",[75,379,143],{"style":81},[75,381,146],{"style":89},[75,383,34],{"style":81},[75,385,386],{"style":85},"input",[75,388,225],{"style":81},[75,390,228],{"style":81},[75,392,393],{"style":89},"slotInputSchema",[75,395,233],{"style":81},[11,397,398,399,403],{},"See ",[238,400,402],{"href":401},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Fcustomization#encode-custom-types","Encode custom types"," for a full example.",[36,405,407],{"id":406},"response-types-per-status-code","Response types per status code",[11,409,410,412,413,416],{},[15,411,17],{}," also writes a ",[15,414,415],{},"ResponsesSchema"," for each operation, with one schema per status code, and its inferred type.",[65,418,421],{"language":67,"filename":419,"code":420,"class":70},"src\u002Fgen\u002Fzod\u002FlistPetsSchema.ts","export const listPetsResponsesSchema = z.object({\n  '200': listPetsStatus200Schema,\n})\n\nexport type ListPetsResponsesSchemaType = z.infer\u003Ctypeof listPetsResponsesSchema>",[15,422,423,101,442,101,456,101,460,101,462],{"class":73},[75,424,425,427,429,432,434,436,438,440],{"class":77,"style":78},[75,426,134],{"style":81},[75,428,137],{"style":81},[75,430,431],{"style":89},"listPetsResponsesSchema ",[75,433,143],{"style":81},[75,435,146],{"style":89},[75,437,34],{"style":81},[75,439,151],{"style":108},[75,441,154],{"style":85},[75,443,444,446,449,451,454],{"class":77,"style":78},[75,445,159],{"style":85},[75,447,448],{"style":99},"'200'",[75,450,117],{"style":81},[75,452,453],{"style":89},"listPetsStatus200Schema",[75,455,177],{"style":81},[75,457,458],{"class":77,"style":78},[75,459,123],{"style":85},[75,461],{"class":77,"style":78},[75,463,464,466,468,471,473,475,477,479,481,483,486],{"class":77,"style":78},[75,465,134],{"style":81},[75,467,210],{"style":81},[75,469,470],{"style":108},"ListPetsResponsesSchemaType ",[75,472,143],{"style":81},[75,474,146],{"style":89},[75,476,34],{"style":81},[75,478,222],{"style":85},[75,480,225],{"style":81},[75,482,228],{"style":81},[75,484,485],{"style":89},"listPetsResponsesSchema",[75,487,233],{"style":81},[11,489,490,26,495,500,501,504,505,507],{},[238,491,493],{"href":492},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch",[15,494,301],{},[238,496,498],{"href":497},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios",[15,499,308],{}," key their ",[15,502,503],{},"RequestResult"," on it when ",[15,506,33],{}," is absent.",[36,509,511],{"id":510},"type-guards-with-inferred-types","Type guards with inferred types",[11,513,42,514,520,521,523,524,526,527,533,534,26,537,34],{},[238,515,517],{"href":516},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Foptions#typeguards",[15,518,519],{},"typeGuards",", which needs Zod 4.6 or later, and ",[15,522,45],{},", the guards narrow to the generated type, such as ",[15,525,60],{},". With ",[238,528,530],{"href":529},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Foptions#mini",[15,531,532],{},"mini: true",", they call ",[15,535,536],{},"z.validate",[15,538,539],{},"z.parse",[65,541,543],{"language":67,"filename":126,"code":542,"class":70},"export const isPet = (data: unknown): data is PetSchemaType => petSchema.validate(data)",[15,544,545],{"class":73},[75,546,547,549,551,554,556,559,562,564,567,570,572,575,578,580,583,585,587,590,592,594],{"class":77,"style":78},[75,548,134],{"style":81},[75,550,137],{"style":81},[75,552,553],{"style":89},"isPet ",[75,555,143],{"style":81},[75,557,558],{"style":85},"(",[75,560,561],{"style":89},"data",[75,563,117],{"style":89},[75,565,566],{"style":108},"unknown",[75,568,569],{"style":85},")",[75,571,117],{"style":81},[75,573,574],{"style":89},"data ",[75,576,577],{"style":81},"is ",[75,579,213],{"style":108},[75,581,582],{"style":81},"=> ",[75,584,56],{"style":89},[75,586,34],{"style":81},[75,588,589],{"style":108},"validate",[75,591,558],{"style":85},[75,593,561],{"style":89},[75,595,569],{"style":85},{"title":597,"searchDepth":598,"depth":598,"links":599},"",5,[600,602,603,604],{"id":38,"depth":601,"text":39},2,{"id":244,"depth":601,"text":245},{"id":406,"depth":601,"text":407},{"id":510,"depth":601,"text":511},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Finferred-types",1794776439754]