§

JSON sample

Type inferrer तैयार हो रहा है…
§

TypeScript interfaces

TypeScript

TypeScript teams को यह समस्या जल्दी आती है। बड़े SDKs typed clients ship करते हैं (Stripe, Twilio, AWS), लेकिन internal services और third-party webhook payloads कम ही करते हैं, इसलिए सामान्य workflow है: network panel में real response capture करें, यहाँ paste करें, root को endpoint के नाम पर name करें, और output project की types directory में copy करें। वहाँ से, strict mode वे mismatches catch करता है जो documentation mention करना भूल गई। Infosys, TCS और Wipro जैसी IT services companies के साथ भारतीय developers US, EU और APAC APIs के लिए microservice boundaries पर JSON shapes validate करने के लिए inferred interfaces use करते हैं बिना bundle में runtime schema dependency जोड़े। Inferrer पूरी तरह आपके browser में चलता है, इसलिए staging APIs, signed webhook bodies और unreleased endpoints के payloads कभी किसी hosted service तक नहीं पहुँचते।

JSON-to-TypeScript inference कैसे काम करती है

Inference parsed JSON tree का single pass है। टूल हर value पढ़ता है, उसके लिए TypeScript type pick करता है, और फिर मिले हर object के लिए एक interface लिखता है।

  1. Browser के native parser से JSON sample parse करें और malformed input को line/column hint के साथ reject करें।
  2. हर value के लिए TypeScript type sniff करें — string, number, boolean, null, array, या nested object।
  3. हर nested object को उसके parent property key से derived interface name दें (इसलिए user.address एक Address interface बन जाता है)।
  4. हर array में item types merge करें ताकि {id: 1} और {id: 2, label: "x"} की list सही optional fields के साथ union produce करे।
  5. आपके options (interface vs. type, readonly, optional-nullable) apply करें और declarations dependency order में emit करें ताकि file बिना forward references के compile हो।

JSON से TypeScript types generate क्यों करें?

  • अधिकतर shape bugs compile time पर catchable हैं यदि response type लिखा हो। Real payload से interface infer करना उसका अधिकतर हिस्सा लिख देता है, और `strict` mode वह field catch करता है जिसे docs mention करना भूल गई।
  • Inferred interfaces को Zod या io-ts जैसे runtime validator के साथ pair करना same shape को दो jobs देता है: development में editor autocomplete और production में unexpected कुछ भेजने पर edge पर 400।
  • TypeScript का language server केवल वे fields surface करता है जो उसे पता हों। एक बार inferred interface import करने के बाद, dot type करते ही autocomplete काम करता है — response पर कोई `as any` cast नहीं और repo में frustrated grep नहीं।
  • यदि आप OpenAPI spec लिखने वाले हैं, तो inferred interface response schema का fast first draft है। Property names और types पहले से correct होंगे, हालाँकि hand-written examples और constraints अभी भी चाहिए होंगे।

सामान्य अनुप्रयोग

Inference तब सबसे ज़्यादा मदद करती है जब real payload exists हो लेकिन schema नहीं।

  • Handler लिखने से पहले Stripe, GitHub या Twilio के third-party webhook payloads type करना।
  • Internal REST API के लिए types bootstrap करना ताकि frontend team उसी दिन उसके against coding शुरू कर सके जिस दिन backend land करे।
  • Observed API response से Zod, io-ts या Valibot schema के लिए starting point generate करना।

Output कैसा दिखता है?

एक sample JSON document और root name दिए जाने पर, generator interfaces का tree produce करता है, प्रत्येक nested object के लिए एक। Root name User के साथ नीचे दिए input के लिए:

{"id":1,"name":"Alice","tags":["a","b"],"address":{"city":"Paris"}} को root name User के साथ paste करें और generator produce करता है:

export interface User {
  id: number;
  name: string;
  tags: string[];
  address: Address;
}

export interface Address {
  city: string;
}
ध्यान दें कि address को अपने named interface में promote किया गया — यही dependency-ordered output है। Same JSON के साथ type declaration style में export type User = {...} emit होगा; readonly toggle चालू होने पर हर property को readonly modifier मिलता है।

Generator options

Declaration style

interface (object shapes के लिए standard TypeScript idiom) या type (handy अगर बाद में mapped types, conditional types, या intersections चाहिए) pick करें। दोनों identical runtime behaviour produce करते हैं; choice एक coding-style preference है।

Optional nullable fields

जब sampled value null हो, field का type T | null हो जाता है। यह option चालू करने पर ? modifier भी जुड़ता है ताकि field TypeScript side पर optional हो — तब useful जब API कभी-कभी null return करने की बजाय key entirely omit करती है।

Readonly modifier

हर property declaration में readonly prepend करता है ताकि emitted interface immutable data model match करे। Redux state slices, frozen API responses, या anywhere के लिए handy जहाँ compiler accidental mutation flag करे।

क्या यह nested objects और arrays support करता है?

हाँ। हर nested object अपने parent property key से derived एक named interface बन जाता है, और arrays अपने contents से item type infer करते हैं। Objects के arrays को object shape per interface मिलती है, union types जहाँ shapes disagree करती हैं।

Optional fields कैसे infer होते हैं?

"Mark null-able fields optional" toggle चालू करें और कोई भी field जिसकी sampled value null है उसे key पर ? modifier और type में | null मिलता है। Toggle बिना, field required रहता है और type बस T | null है।

क्या यह discriminated unions support करता है?

Basic union types तब आते हैं जब array mixed-shape items hold करे या field दोनों value और null carry करे। Full discriminated-union inference (tag के रूप में type या kind pick करना और variants split करना) multiple samples चाहता है — वह planned है लेकिन आज के build में नहीं।

क्या मैं multiple JSON samples से types infer कर सकता हूँ?

अभी नहीं — आज का inferrer एक बार में एक sample पढ़ता है। यदि आपके पास दो payloads हैं जो एक interface share करनी चाहिए (मान लें, list endpoint और single-item endpoint), तो practical workaround है उन्हें एक array में merge करें, उससे generate करें, और resulting union types rename करें। Multi-sample inference roadmap पर है क्योंकि यही एकमात्र तरीका है उन fields spot करने का जो एक response में present हैं और दूसरे में absent।

Payload paste करें, root name दें, interfaces copy करें। पूरी pipeline आपके ब्राउज़र में चलती है, इसलिए unreleased API या signed webhook body आपकी machine पर रहती है।