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

TypeScript interface डालें और उदाहरण JSON बनाएँ! TypeScript interface, type समर्थित। API डॉक्यूमेंटेशन और टेस्ट request body (sample JSON) के लिए उपयोगी।
TypeScript
परिणाम
 

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

TypeScript interface, type alias, enum या class declarations पेस्ट करें और उन्हें संतुष्ट करने वाला उदाहरण JSON document पाएं। Literal unions, Record, Pick, Omit और Partial जैसे utility types, और extends से जुड़े interfaces, सबका पीछा करके उन्हें भरा जाता है।

इसे API बनने से पहले MSW या json-server mock data तैयार करने, Storybook props बनाने, या backend टीम को ठीक-ठीक दिखाने के लिए इस्तेमाल करें कि आप उनसे कौन सा आकार चाहते हैं।

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

  1. types.ts जैसी फ़ाइल से type declarations पेस्ट करें। export, import और function declarations मिले हो सकते हैं; सिर्फ़ type declarations पढ़े जाते हैं।
  2. Reference होने वाले types और enums शामिल करें ताकि वे भरें। इनपुट में न मौजूद type नाम null बनते हैं।
  3. कोई options नहीं हैं। जिस पहले object type को कोई दूसरा declaration reference नहीं करता वह root बनता है, इसलिए जो चाहिए उसे पहले रखें; generic भरने वाला alias, जैसे type UserPage = Page<User>, generic declaration से ज़्यादा प्राथमिकता पाता है।
  4. परिणाम को MSW handler के HttpResponse.json(...), test fixture या Storybook args में कॉपी करें, फिर values ठीक करें।

उदाहरण

इनपुट (TypeScript)
type Channel = "email" | "sms" | "push";

enum Priority { Low = "low", High = "high" }

interface Notification {
  id: string;
  channel: Channel;
  priority: Priority;
  recipient: { userId: number; locale?: string };
  payload: Record<string, string>;
  readAt: Date | null;
  meta: Pick<Audit, "createdBy">;
}

interface Audit { createdBy: string; createdAt: Date }
आउटपुट (JSON)
{
  "id": "",
  "channel": "email",
  "priority": "low",
  "recipient": {
    "userId": 0,
    "locale": ""
  },
  "payload": {
    "key": ""
  },
  "readAt": "1970-01-01T00:00:00.000Z",
  "meta": {
    "createdBy": ""
  }
}

Channel string literal union है, इसलिए वह अपनी पहली वैल्यू "email" लेता है; string enum Priority अपने पहले member की वैल्यू "low" लेता है। Record<string, string> एक "key" वाला object बनता है, Date | null non-null तरफ़ resolve होकर ISO string बनता है, और Pick<Audit, "createdBy"> सिर्फ़ चुनी हुई key रखता है। Optional locale? भी शामिल है।

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

  • Primitives: string → "", number/bigint → 0, boolean → false, any/unknown → null, Date → 1970-01-01T00:00:00.000Z (Date.toJSON() का format)।
  • Literal types अपनी वैल्यू इस्तेमाल करते हैं, unions पहला ऐसा member लेते हैं जो null/undefined न हो, और intersections (A & B) व interface ... extends properties मिलाते हैं।
  • enum अपने पहले member की वैल्यू इस्तेमाल करता है, या बिना initializers वाले numeric enum के लिए 0।
  • Arrays, Array<T> और Set<T> को एक sample element मिलता है; tuples को हर position के लिए एक वैल्यू मिलती है; Record<K, V>, Map और index signatures {"key": V} बनते हैं।
  • Utility types Partial, Required, Readonly, NonNullable, Pick और Omit समर्थित हैं।
  • Methods और function-typed properties JSON में नहीं होते और हटा दिए जाते हैं; classes में static fields, get/set accessors और @Exclude() fields बाहर रहते हैं।
  • Self-referencing arrays (children: TreeNode[]) खाली arrays के रूप में काट दिए जाते हैं।

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

MSW या json-server के लिए mock responses

Backend तैयार होने से पहले API response types से mock data बनाएं। UI पहले बन सकती है, और बाद में असली API पर जाने पर वही types बने रहते हैं।

Storybook और tests के लिए props

Story args या React Testing Library render data के लिए component के props type से default object बनाएं। हर required property मौजूद होती है, इसलिए पीछा करने को कोई type errors नहीं।

Backend के साथ request formats पर सहमति

Frontend request type को JSON में बदलकर API design doc या issue में पेस्ट करें, ताकि TypeScript इस्तेमाल न करने वाले backend डेवलपर्स संरचना तुरंत पढ़ सकें।

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

  • Optional properties (?) हमेशा शामिल होती हैं; जिन responses में वे न हों उन्हें test करने के लिए keys खुद हटाएं।
  • Generic parameters वहां बदले जाते हैं जहां type इस्तेमाल होता है: type UserPage = Page<User> के साथ items: T[] User से भरा जाता है, और कोई argument न होने पर <T = string> जैसे defaults लागू होते हैं। Generics के अंदर के conditional types फिर भी evaluate नहीं होते।
  • T["key"] जैसे indexed access types, conditional types और template literal types evaluate नहीं होते।
  • एक literal से initialize किया class field (name = "kim", retries = 3) वही वैल्यू इस्तेमाल करता है; new Date() जैसे दूसरे initializers सिर्फ़ type का संकेत देते हैं।
  • Zod या io-ts जैसा runtime schema कोड नहीं पढ़ा जाता; z.infer से अनुमानित type को सादे interface के रूप में लिखें।
  • Map और Set object और array के रूप में दिखते हैं, लेकिन JSON.stringify दोनों को {} बना देता है, इसलिए response types में Record या arrays बेहतर हैं।

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

क्या Date का string के रूप में आना सही है?

हां। JSON.stringify Date को उसके toJSON() परिणाम, यानी ISO 8601 string, से बदल देता है। पाने वाली तरफ़ फिर भी string मिलती है, इसलिए या तो उसे string type दें या parse करें।

status: "active" | "inactive" जैसे union को कौन सी वैल्यू मिलती है?

Declaration क्रम में पहला literal, "active"। कोई दूसरी वैल्यू दिखानी हो तो union का क्रम बदलें।

क्या यह NestJS DTO classes के साथ काम करता है?

Class declarations और decorators parse होते हैं, इसलिए class-validator decorators वाले DTOs ठीक से बदलते हैं, और class-transformer के @Exclude() fields बाहर रहते हैं।

JSON से TypeScript types कैसे बनाऊं?

उल्टी दिशा के लिए JSON → TypeScript पेज इस्तेमाल करें। TypeORM, Sequelize और Mongoose models से JSON बनाने के लिए भी अलग पेज हैं।