JSON को TypeScript टाइप में बदलें!

JSON डालें और उसे TypeScript interface में बदलें! TypeScript interface समर्थित। TypeORM · Sequelize · Mongoose ऊपर के टैब में।
JSON
परिणाम
 

JSON को TypeScript टाइप में बदलें!

यह टूल JSON वैल्यू पढ़कर उसके लिए TypeScript interface declarations लिखता है। Keys ठीक वैसी ही रहती हैं; objects के arrays एक interface में मिलाए जाते हैं, सिर्फ़ कुछ items में मौजूद keys को ? मिलता है, और मिली-जुली values union types बनती हैं।

इसे frontend पर fetch या axios responses को type करने, Next.js API routes या Express handlers के return types declare करने, या जब भी backend सिर्फ़ उदाहरण responses document करे और type definitions न दे, तब इस्तेमाल करें।

उपयोग कैसे करें

  1. Response JSON पेस्ट करें, उदाहरण के लिए DevTools के Network टैब से कॉपी किया हुआ।
  2. टाइप का नाम में root interface का नाम डालें (जैसे ChannelHistory); यह URL में ?name= के रूप में रहता है।
  3. आउटपुट को types.ts जैसी फ़ाइल में सेव करें और import type { ChannelHistory } from "./types" से इस्तेमाल करें।

उदाहरण

इनपुट (JSON)
{
  "channel": "general",
  "messages": [
    { "id": "m1", "user": "kim", "text": "deploy done", "reactions": ["+1"] },
    { "id": "m2", "user": "lee", "text": null, "edited": true, "attachment": { "type": "image", "url": "https://cdn.example.com/a.png" } }
  ],
  "cursor": 42
}
आउटपुट (TypeScript)
export interface ChannelHistory {
  channel: string;
  messages: Message[];
  cursor: number;
}

export interface Message {
  id: string;
  user: string;
  text: string | null;
  reactions?: string[];
  edited?: boolean;
  attachment?: Attachment;
}

export interface Attachment {
  type: string;
  url: string;
}

दोनों messages एक Message interface में मिल जाते हैं; reactions, edited और attachment उनमें से सिर्फ़ एक में हैं, इसलिए उन्हें ? मिलता है। text में string और null दोनों हैं, जिससे string | null बनता है। Element interfaces key का एकवचन रूप लेते हैं (messages → Message)।

समर्थित सुविधाएँ

  • हर declaration export interface है, जो कहीं और import करने के लिए तैयार है।
  • JSON keys rename नहीं होतीं, इसलिए types response object से एक-एक मेल खाते हैं; जो keys वैध identifiers नहीं हैं ("a-b", "2fa") वे quote होती हैं।
  • Objects के arrays हर element की keys मिलाते हैं; कुछ elements में न मौजूद keys optional (?:) बनती हैं।
  • मिले-जुले types unions (string | number) बनाते हैं; null सबसे आखिर में | null के रूप में जुड़ता है। मिले-जुले arrays (string | number)[] बनते हैं।
  • खाली arrays में अनुमान लगाने के लिए कोई element type नहीं होता और वे unknown[] बनते हैं।
  • Top-level array export type Root = RootItem[]; जैसा alias भी बनाता है।

उपयोग के मामले

fetch और axios responses को type करना

Type को generic के रूप में दें, जैसे const res = await axios.get<ChannelHistory>(url), और editor चेतावनी देता है कि res.data.messages[0].text null हो सकता है।

बिना typings वाले third-party APIs

जिन internal या बाहरी APIs का कोई SDK या @types package नहीं है, उनके लिए टीम के साथ साझा करने लायक type definition फ़ाइल बनाने को एक sample response काफ़ी है।

Mock data और component props

Storybook या test mock JSON से interfaces बनाएं और उन्हें component props types के रूप में इस्तेमाल करें; जब mocks components से अलग हो जाएं, compiler आपको बताता है।

नोट्स और सीमाएँ

  • Interfaces compile time पर मिट जाते हैं और runtime पर यह नहीं जांचते कि response का सच में वही आकार है। Runtime validation चाहिए तो zod जैसी schema library के साथ इस्तेमाल करें।
  • सभी संख्याएं number हैं (integer/float का अलगाव नहीं), और date strings string हैं।
  • "status": "active" जैसे तय values वाले fields string के रूप में आते हैं; उन्हें literal unions ("active" | "banned") में खुद संकरा करें।
  • अलग-अलग keys के तहत एक जैसे आकारों को हर एक को अपना interface मिलता है, और टकराते नामों को Profile2 जैसा numeric suffix मिलता है।

अक्सर पूछे जाने वाले प्रश्न

क्या interfaces की बजाय type aliases मिल सकते हैं?

type alias सिर्फ़ top-level array या primitive के लिए इस्तेमाल होता है; objects हमेशा interface होते हैं। इस उद्देश्य के लिए export interface X { ... } को export type X = { ... }; के रूप में दोबारा लिखना एक जैसा व्यवहार करता है।

Optional (?) और | null अलग क्यों रखे जाते हैं?

इनके मतलब अलग हैं: ? कहता है कि key गायब हो सकती है (undefined), जबकि | null कहता है कि key null वैल्यू के साथ मौजूद है। strictNullChecks चालू होने पर आप दोनों स्थितियों को अलग तरह से संभालते हैं।

Keys camelCase में क्यों नहीं बदलतीं?

TypeScript type असली object को दर्शाता है, इसलिए rename हुई keys response से मेल नहीं खाएंगी। अगर अपने कोड में camelCase चाहिए, तो अपने mapping function के परिणाम के लिए अलग type define करें।

मुझे ORM entities (TypeORM, Sequelize, Mongoose) चाहिए।

यह पेज सिर्फ़ सादे type declarations बनाता है। ऊपर के TypeORM, Sequelize और Mongoose टैब उसी JSON से entity और model कोड बनाते हैं।