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