UTILS.
100% ब्राउज़र में
📘

JSON Schema से TypeScript

एक JSON Schema (draft-07 या 2020-12) को TypeScript interfaces और type aliases में compile करें, required, enums, $ref, arrays, और combinators को संभालते हुए।

आउटपुट

इस टूल के बारे में

JSON Schema से TypeScript टूल एक JSON Schema document को TypeScript interfaces और type aliases में compile करता है, वह करते हुए जो लोकप्रिय json-schema-to-typescript library करती है, बिना किसी toolchain को इंस्टॉल किए। JSON-नमूना कन्वर्टर्स के विपरीत, यह एक वास्तविक schema पढ़ता है: यह schema object को चलता है और type:'object' को एक interface में बदल देता है जहाँ required में सूचीबद्ध keys non-optional होती हैं और बाकी ? से चिह्नित होती हैं।

सब कुछ आपके ब्राउज़र में स्थानीय रूप से JSON.parse और सादे JavaScript के साथ पार्स और जनरेट होता है, इसलिए आपका schema कभी पेज नहीं छोड़ता। enum वाली String schemas string literals का एक union बन जाती हैं; plain string, number/integer, boolean, और null अपने TypeScript समकक्षों पर मैप होते हैं; arrays T[] बन जाती हैं (एक tuple जब items एक array हो)। Combinators समर्थित हैं — allOf एक intersection (A & B) बन जाता है और anyOf / oneOf unions (A | B) बन जाते हैं — और एक nullable type:[T,'null'] T | null बन जाता है।

References को नाम से हल किया जाता है: #/definitions/X या #/$defs/X पर इंगित करने वाला $ref एक नामित type reference निकालता है, और हर definition अपने स्वयं के export interface या export type के रूप में जनरेट होती है। additionalProperties एक index signature [key: string]: T जोड़ता है। root type नाम schema के title या $id पर fall back करता है। interface बनाम type आउटपुट और क्या schema description strings JSDoc /** */ comments बनें, यह toggle करें। परिणाम को कॉपी करें या एक .ts फ़ाइल डाउनलोड करें।

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

कौन सी JSON Schema विशेषताएँ समर्थित हैं?
properties और required वाला object, string enums को literal unions के रूप में, number/integer/boolean/null, arrays (tuples सहित), allOf/anyOf/oneOf combinators, definitions या $defs के लिए $ref, additionalProperties को एक index signature के रूप में, और ["string","null"] जैसी nullable type arrays।
$ref और definitions को कैसे संभाला जाता है?
#/definitions/Address (या #/$defs/Address) जैसा एक $ref एक नामित type Address के reference में बदल जाता है, और definitions या $defs के अंतर्गत प्रत्येक entry अपने स्वयं के export interface या export type के रूप में निकाली जाती है, ताकि references आउटपुट के भीतर हल हों।
root type का नाम क्या रखता है?
root-name box यदि आप इसे सेट करते हैं, अन्यथा schema का title, अन्यथा इसके $id का अंतिम खंड, अन्यथा Root। Definition नाम definitions या $defs में उनकी key से आते हैं, PascalCase में बदले हुए।
क्या मैं schema descriptions रख सकता हूँ?
हाँ। JSDoc चालू होने पर (डिफ़ॉल्ट), schema या किसी property पर कोई भी description string संबंधित type या field के ऊपर एक /** ... */ comment के रूप में निकाली जाती है, जनरेट किए गए TypeScript में documentation संरक्षित करते हुए।

और टूल