§

JSON ಮಾದರಿ

Type inferrer ತಯಾರಿಸಲಾಗುತ್ತಿದೆ…
§

TypeScript ಇಂಟರ್ಫೇಸ್‌ಗಳು

TypeScript

ಬೆಂಗಳೂರಿನ TypeScript teams ಈ ಸಮಸ್ಯೆಯನ್ನು ಆಗಾಗ ಎದುರಿಸುತ್ತಾರೆ. Flipkart, Ola, ಮತ್ತು Razorpay ನಂತಹ ಕಂಪನಿಗಳ internal API responses ಮತ್ತು payment webhook payloads ಅಪರೂಪವಾಗಿ typed clients ಸಮೇತ ಬರುತ್ತವೆ. Network panel ನಲ್ಲಿ real response capture ಮಾಡಿ, ಇಲ್ಲಿ paste ಮಾಡಿ, endpoint ನ ಹೆಸರಿನ ಮೇರೆಗೆ root ಹೆಸರಿಸಿ, ಮತ್ತು output ಅನ್ನು project ನ types directory ಗೆ copy ಮಾಡಿ — ಅಷ್ಟೆ. Inferrer ಸಂಪೂರ್ಣವಾಗಿ ನಿಮ್ಮ ಬ್ರೌಸರ್‌ನಲ್ಲಿ ನಡೆಯುತ್ತದೆ, ಆದ್ದರಿಂದ staging APIs, signed webhook bodies ಮತ್ತು unreleased endpoints ನ payloads ಯಾವ hosted service ತಲುಪುವುದಿಲ್ಲ.

JSON-to-TypeScript inference ಹೇಗೆ ಕಾರ್ಯನಿರ್ವಹಿಸುತ್ತದೆ

Inference parsed JSON tree ಮೇಲೆ ಒಂದೇ pass ಆಗಿದೆ. ಟೂಲ್ ಪ್ರತಿ ಮೌಲ್ಯ ಓದುತ್ತದೆ, ಅದಕ್ಕೆ TypeScript type ಆಯ್ಕೆ ಮಾಡುತ್ತದೆ, ಮತ್ತು ಕಂಡ ಪ್ರತಿ object ಗೆ ಒಂದು interface ಬರೆಯುತ್ತದೆ.

  1. ಬ್ರೌಸರ್ ನ native parser ಮೂಲಕ JSON sample parse ಮಾಡಿ ಮತ್ತು malformed input ಅನ್ನು line/column hint ಸಮೇತ reject ಮಾಡಿ.
  2. ಪ್ರತಿ ಮೌಲ್ಯಕ್ಕೆ TypeScript type sniff ಮಾಡಿ — string, number, boolean, null, array, ಅಥವಾ nested object.
  3. ಪ್ರತಿ nested object ಗೆ ಅದರ parent property key ನಿಂದ ಪಡೆದ interface ಹೆಸರು ನೀಡಿ (ಹೀಗೆ user.address Address interface ಆಗುತ್ತದೆ).
  4. ಪ್ರತಿ array ಯಾದ್ಯಂತ item types merge ಮಾಡಿ ಇದರಿಂದ {id: 1} ಮತ್ತು {id: 2, label: "x"} ಪಟ್ಟಿ ಸರಿಯಾದ optional fields ಸಮೇತ union ತಯಾರಿಸುತ್ತದೆ.
  5. ನಿಮ್ಮ options (interface vs. type, readonly, optional-nullable) apply ಮಾಡಿ ಮತ್ತು declarations ಅನ್ನು dependency order ನಲ್ಲಿ emit ಮಾಡಿ ಇದರಿಂದ file forward references ಇಲ್ಲದೆ compile ಆಗುತ್ತದೆ.

JSON ನಿಂದ TypeScript types ಏಕೆ generate ಮಾಡಬೇಕು?

  • Response type ಬರೆದಿದ್ದರೆ ಹೆಚ್ಚಿನ shape bugs compile time ನಲ್ಲಿ ಹಿಡಿಯಬಹುದು. Real payload ನಿಂದ interface infer ಮಾಡುವುದು ಅದರ ಹೆಚ್ಚಿನ ಭಾಗ ನಿಮಗಾಗಿ ಬರೆಯುತ್ತದೆ, ಮತ್ತು `strict` mode docs ಮರೆತ field catch ಮಾಡುತ್ತದೆ.
  • Inferred interfaces ಅನ್ನು Zod ಅಥವಾ io-ts ನಂತಹ runtime validator ಜೊತೆ ಜೋಡಿಸುವುದರಿಂದ ಒಂದೇ shape ಎರಡು ಕೆಲಸ ಮಾಡುತ್ತದೆ: development ನಲ್ಲಿ editor autocomplete ಮತ್ತು production ನಲ್ಲಿ ಅನಿರೀಕ್ಷಿತ ಕಳುಹಿಸಿದಾಗ edge ನಲ್ಲಿ 400.
  • TypeScript ನ language server ಅದು ತಿಳಿದ fields ಮಾತ್ರ surface ಮಾಡುತ್ತದೆ. Inferred interface import ಮಾಡಿದ ತಕ್ಷಣ dot ಟೈಪ್ ಮಾಡಿದ ಕ್ಷಣ autocomplete ಕಾರ್ಯ ನಿರ್ವಹಿಸುತ್ತದೆ — response ಮೇಲೆ `as any` cast ಮತ್ತು repo ಯಾದ್ಯಂತ frustrated grep ಇನ್ನಿಲ್ಲ.
  • OpenAPI spec ಬರೆಯಲಿದ್ದರೆ, inferred interface response schema ನ ತ್ವರಿತ ಮೊದಲ ಕರಡು. Property ಹೆಸರುಗಳು ಮತ್ತು types ಈಗಾಗಲೇ ಸರಿ; hand-written examples ಮತ್ತು constraints ಇನ್ನೂ ಬೇಕು.

ಸಾಮಾನ್ಯ ಅನ್ವಯಗಳು

Real payload ಇರುವಾಗ ಆದರೆ schema ಇಲ್ಲದಿರುವಾಗ inference ಹೆಚ್ಚು ಸಹಾಯ ಮಾಡುತ್ತದೆ.

  • Handler ಬರೆಯುವ ಮೊದಲು Stripe, GitHub, ಅಥವಾ Twilio ನ third-party webhook payloads ಅನ್ನು type ಮಾಡುವುದು — real webhook sample ಅನ್ನು ಇಲ್ಲಿ paste ಮಾಡಿ ತಕ್ಷಣ typed interface ಪಡೆಯಿರಿ.
  • Internal REST API ಗೆ types bootstrap ಮಾಡುವುದು ಇದರಿಂದ frontend team backend ಬಂದ ದಿನವೇ ಅದರ ವಿರುದ್ಧ coding ಶುರು ಮಾಡಬಹುದು.
  • Observed API response ನಿಂದ Zod, io-ts, ಅಥವಾ Valibot schema ಗೆ starting point generate ಮಾಡುವುದು — inferred interface ಅನ್ನು ಆಧಾರವಾಗಿ runtime validation schema ಬರೆಯಲು ಉಪಯೋಗಿಸಿ.

ಔಟ್‌ಪುಟ್ ಹೇಗೆ ಕಾಣುತ್ತದೆ?

JSON document sample ಮತ್ತು root ಹೆಸರು ಕೊಟ್ಟಾಗ, generator ಒಂದು interfaces tree ತಯಾರಿಸುತ್ತದೆ, ಪ್ರತಿ nested object ಗೆ ಒಂದು. Root ಹೆಸರು User ಸಮೇತ ಕೆಳಗಿನ input ಗಾಗಿ:

Root ಹೆಸರು User ಸಮೇತ {"id":1,"name":"Alice","tags":["a","b"],"address":{"city":"Paris"}} paste ಮಾಡಿ generator ತಯಾರಿಸುತ್ತದೆ:

export interface User {
  id: number;
  name: string;
  tags: string[];
  address: Address;
}

export interface Address {
  city: string;
}
address ತನ್ನದೇ named interface ಗೆ promote ಆಗಿದೆ ಗಮನಿಸಿ — ಅದು dependency-ordered output. ಅದೇ JSON type declaration style ಸಮೇತ export type User = {...} emit ಮಾಡುತ್ತದೆ; readonly toggle ಚಾಲು ಸಮೇತ ಪ್ರತಿ property readonly modifier ಪಡೆಯುತ್ತದೆ.

ಜನರೇಟರ್ ಆಯ್ಕೆಗಳು

ಡಿಕ್ಲರೇಶನ್ ಶೈಲಿ

Object shapes ಗಾಗಿ standard TypeScript idiom ಆದ interface ಅಥವಾ ನಂತರ mapped types, conditional types, ಅಥವಾ intersections ಬೇಕಾದರೆ ಉಪಯುಕ್ತ type ಆಯ್ಕೆ ಮಾಡಿ. ಎರಡೂ runtime behaviour ಒಂದೇ; ಆಯ್ಕೆ coding-style preference.

ಐಚ್ಛಿಕ ಶೂನ್ಯ ಫೀಲ್ಡ್‌ಗಳು

Sampled ಮೌಲ್ಯ null ಆದಾಗ, field ನ type T | null ಆಗುತ್ತದೆ. ಈ option ಚಾಲು ಮಾಡುವುದರಿಂದ ? modifier ಕೂಡ ಸೇರಿಸಲ್ಪಡುತ್ತದೆ ಇದರಿಂದ field TypeScript side ನಲ್ಲಿ optional ಆಗುತ್ತದೆ — API ಕೆಲವೊಮ್ಮೆ null return ಮಾಡುವ ಬದಲು key ಸಂಪೂರ್ಣ ಬಿಡಿಸಿದಾಗ ಉಪಯುಕ್ತ.

Readonly ಮಾರ್ಪಾಡು

ಪ್ರತಿ property declaration ಗೆ readonly prepend ಮಾಡುತ್ತದೆ ಇದರಿಂದ emitted interface immutable data model ಗೆ ಹೊಂದಿಕೊಳ್ಳುತ್ತದೆ. Redux state slices, frozen API responses, ಅಥವಾ compiler ಅಕ್ರಮ mutation flag ಮಾಡಬೇಕಾದ ಯಾವ ಕಡೆಗೂ ಉಪಯುಕ್ತ.

ಇದು nested objects ಮತ್ತು arrays ಅನ್ನು ಬೆಂಬಲಿಸುತ್ತದೆಯೇ?

ಹೌದು. ಪ್ರತಿ nested object ಅದರ parent property key ನಿಂದ ಪಡೆದ named interface ಆಗುತ್ತದೆ, ಮತ್ತು arrays ತಮ್ಮ contents ನಿಂದ item type infer ಮಾಡುತ್ತವೆ. Objects ನ arrays ಪ್ರತಿ object shape ಗೆ interface ಪಡೆಯುತ್ತವೆ, shapes ಭಿನ್ನವಾದಲ್ಲಿ union types ಸಮೇತ.

ಐಚ್ಛಿಕ fields ಅನ್ನು ಹೇಗೆ infer ಮಾಡಲಾಗುತ್ತದೆ?

"Mark null-able fields optional" toggle ಚಾಲು ಮಾಡಿ ಮತ್ತು sampled ಮೌಲ್ಯ null ಆಗಿರುವ ಯಾವ field ಕೂಡ key ಮೇಲೆ ? modifier ಮತ್ತು type ನಲ್ಲಿ | null ಪಡೆಯುತ್ತದೆ. Toggle ಇಲ್ಲದಿದ್ದರೆ, field required ಉಳಿಯುತ್ತದೆ ಮತ್ತು type ಕೇವಲ T | null.

ಇದು discriminated unions ಅನ್ನು ಬೆಂಬಲಿಸುತ್ತದೆಯೇ?

Array mixed-shape items ಹೊಂದಿದ್ದಾಗ ಅಥವಾ field ಮೌಲ್ಯ ಮತ್ತು null ಎರಡೂ ಒಯ್ದಾಗ basic union types ಬರುತ್ತವೆ. Full discriminated-union inference (tag ಆಗಿ type ಅಥವಾ kind ಆಯ್ಕೆ ಮಾಡಿ variants split ಮಾಡುವುದು) multiple samples ಬೇಕು — ಅದು planned ಆದರೆ ಇಂದಿನ build ನಲ್ಲಿ ಇಲ್ಲ.

ಬಹು JSON ಮಾದರಿಗಳಿಂದ ನಾನು types ಅನ್ನು infer ಮಾಡಬಹುದೇ?

ಇನ್ನೂ ಇಲ್ಲ — ಇಂದಿನ inferrer ಒಂದೇ sample ಒಂದೇ ಬಾರಿ ಓದುತ್ತದೆ. ಎರಡು payloads interface share ಮಾಡಬೇಕಿದ್ದರೆ, ಪ್ರಾಯೋಗಿಕ workaround ಎಂದರೆ ಅವುಗಳನ್ನು ಒಂದು array ಗೆ merge ಮಾಡಿ, ಅದರಿಂದ generate ಮಾಡಿ, ನಂತರ resulting union types ಮರುನಾಮಕರಣ ಮಾಡಿ. Multi-sample inference roadmap ನಲ್ಲಿ ಇದೆ.

Payload paste ಮಾಡಿ, root ಹೆಸರಿಸಿ, interfaces ನಕಲಿಸಿ. ಸಂಪೂರ್ಣ pipeline ನಿಮ್ಮ ಬ್ರೌಸರ್‌ನಲ್ಲಿ ನಡೆಯುತ್ತದೆ, ಆದ್ದರಿಂದ unreleased API ಅಥವಾ signed webhook body ನಿಮ್ಮ ಯಂತ್ರದಲ್ಲಿ ಉಳಿಯುತ್ತದೆ.