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 टीम को ठीक-ठीक दिखाने के लिए इस्तेमाल करें कि आप उनसे कौन सा आकार चाहते हैं।
types.ts जैसी फ़ाइल से type declarations पेस्ट करें। export, import और function declarations मिले हो सकते हैं; सिर्फ़ type declarations पढ़े जाते हैं।enums शामिल करें ताकि वे भरें। इनपुट में न मौजूद type नाम null बनते हैं।type UserPage = Page<User>, generic declaration से ज़्यादा प्राथमिकता पाता है।HttpResponse.json(...), test fixture या Storybook args में कॉपी करें, फिर values ठीक करें।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 }{
"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? भी शामिल है।
string → "", number/bigint → 0, boolean → false, any/unknown → null, Date → 1970-01-01T00:00:00.000Z (Date.toJSON() का format)।null/undefined न हो, और intersections (A & B) व interface ... extends properties मिलाते हैं।enum अपने पहले member की वैल्यू इस्तेमाल करता है, या बिना initializers वाले numeric enum के लिए 0।Array<T> और Set<T> को एक sample element मिलता है; tuples को हर position के लिए एक वैल्यू मिलती है; Record<K, V>, Map और index signatures {"key": V} बनते हैं।Partial, Required, Readonly, NonNullable, Pick और Omit समर्थित हैं।static fields, get/set accessors और @Exclude() fields बाहर रहते हैं।children: TreeNode[]) खाली arrays के रूप में काट दिए जाते हैं।Backend तैयार होने से पहले API response types से mock data बनाएं। UI पहले बन सकती है, और बाद में असली API पर जाने पर वही types बने रहते हैं।
Story args या React Testing Library render data के लिए component के props type से default object बनाएं। हर required property मौजूद होती है, इसलिए पीछा करने को कोई type errors नहीं।
Frontend request type को JSON में बदलकर API design doc या issue में पेस्ट करें, ताकि TypeScript इस्तेमाल न करने वाले backend डेवलपर्स संरचना तुरंत पढ़ सकें।
?) हमेशा शामिल होती हैं; जिन responses में वे न हों उन्हें test करने के लिए keys खुद हटाएं।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 नहीं होते।name = "kim", retries = 3) वही वैल्यू इस्तेमाल करता है; new Date() जैसे दूसरे initializers सिर्फ़ type का संकेत देते हैं।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 का क्रम बदलें।
Class declarations और decorators parse होते हैं, इसलिए class-validator decorators वाले DTOs ठीक से बदलते हैं, और class-transformer के @Exclude() fields बाहर रहते हैं।
उल्टी दिशा के लिए JSON → TypeScript पेज इस्तेमाल करें। TypeORM, Sequelize और Mongoose models से JSON बनाने के लिए भी अलग पेज हैं।