यह टूल 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 न दे, तब इस्तेमाल करें।
टाइप का नाम में root interface का नाम डालें (जैसे ChannelHistory); यह URL में ?name= के रूप में रहता है।types.ts जैसी फ़ाइल में सेव करें और import type { ChannelHistory } from "./types" से इस्तेमाल करें।{
"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
}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)।
export interface है, जो कहीं और import करने के लिए तैयार है।"a-b", "2fa") वे quote होती हैं।?:) बनती हैं।string | number) बनाते हैं; null सबसे आखिर में | null के रूप में जुड़ता है। मिले-जुले arrays (string | number)[] बनते हैं।unknown[] बनते हैं।export type Root = RootItem[]; जैसा alias भी बनाता है।Type को generic के रूप में दें, जैसे const res = await axios.get<ChannelHistory>(url), और editor चेतावनी देता है कि res.data.messages[0].text null हो सकता है।
जिन internal या बाहरी APIs का कोई SDK या @types package नहीं है, उनके लिए टीम के साथ साझा करने लायक type definition फ़ाइल बनाने को एक sample response काफ़ी है।
Storybook या test mock JSON से interfaces बनाएं और उन्हें component props types के रूप में इस्तेमाल करें; जब mocks components से अलग हो जाएं, compiler आपको बताता है।
number हैं (integer/float का अलगाव नहीं), और date strings string हैं।"status": "active" जैसे तय values वाले fields string के रूप में आते हैं; उन्हें literal unions ("active" | "banned") में खुद संकरा करें।Profile2 जैसा numeric suffix मिलता है।type aliases मिल सकते हैं?type alias सिर्फ़ top-level array या primitive के लिए इस्तेमाल होता है; objects हमेशा interface होते हैं। इस उद्देश्य के लिए export interface X { ... } को export type X = { ... }; के रूप में दोबारा लिखना एक जैसा व्यवहार करता है।
?) और | null अलग क्यों रखे जाते हैं?इनके मतलब अलग हैं: ? कहता है कि key गायब हो सकती है (undefined), जबकि | null कहता है कि key null वैल्यू के साथ मौजूद है। strictNullChecks चालू होने पर आप दोनों स्थितियों को अलग तरह से संभालते हैं।
TypeScript type असली object को दर्शाता है, इसलिए rename हुई keys response से मेल नहीं खाएंगी। अगर अपने कोड में camelCase चाहिए, तो अपने mapping function के परिणाम के लिए अलग type define करें।
यह पेज सिर्फ़ सादे type declarations बनाता है। ऊपर के TypeORM, Sequelize और Mongoose टैब उसी JSON से entity और model कोड बनाते हैं।