§

JSON sample

Type inferrer prepare…
§

TypeScript interfaces

TypeScript

TypeScript developers internal services third-party webhook payloads typed clients rarely ship. Network panel real response capture, root name, project types directory output copy — strict mode documentation mismatches catch. Browser entirely inferrer, staging APIs, signed webhook bodies, unreleased endpoints hosted service reach never.

JSON થી TypeScript નું અનુમાન કેવી રીતે કામ કરે છે

Inference એ parse કરેલા JSON ટ્રી પર એક જ પાસ છે. આ સાધન દરેક મૂલ્ય વાંચે છે, તેના માટે એક TypeScript પ્રકાર પસંદ કરે છે, અને પછી તેને મળેલા દરેક ઑબ્જેક્ટ માટે એક interface લખી નાખે છે.

  1. બ્રાઉઝરના નેટિવ પાર્સર વડે JSON નમૂનો parse કરો અને ખામીયુક્ત ઇનપુટને પંક્તિ/કૉલમ સંકેત સાથે નકારી કાઢો.
  2. દરેક મૂલ્ય માટે એક TypeScript પ્રકાર ઓળખી કાઢો — string, number, boolean, null, ઍરે, અથવા નેસ્ટેડ ઑબ્જેક્ટ.
  3. દરેક નેસ્ટેડ ઑબ્જેક્ટને તેની પેરન્ટ પ્રોપર્ટી કી પરથી તારવેલું interface નામ આપો (જેથી user.address એક Address interface બની જાય).
  4. દરેક ઍરેમાં item પ્રકારોને merge કરો જેથી {id: 1} અને {id: 2, label: "x"} ની યાદી યોગ્ય વૈકલ્પિક ક્ષેત્રો સાથે એક union ઉત્પન્ન કરે.
  5. તમારા વિકલ્પો (interface vs. type, readonly, optional-nullable) લાગુ કરો અને declarations ને dependency ક્રમમાં emit કરો જેથી ફાઇલ forward references વગર compile થાય.

JSON TypeScript types?

  • જો response પ્રકાર લખેલો હોય તો મોટાભાગના shape બગ compile સમયે પકડી શકાય છે. વાસ્તવિક payload પરથી interface infer કરવાથી તેનો મોટાભાગનો ભાગ તમારા માટે લખાઈ જાય છે, અને `strict` મોડ એ ક્ષેત્ર પકડે છે જેનો ઉલ્લેખ કરવાનું docs ભૂલી ગયા હતા.
  • Infer કરેલા interfaces ને Zod અથવા io-ts જેવા રનટાઇમ validator સાથે જોડવાથી એક જ shape ને બે કામ મળે છે: ડેવલપમેન્ટમાં એડિટર autocomplete અને production જ્યારે કંઈક અણધાર્યું મોકલે ત્યારે edge પર 400.
  • TypeScript નો language server માત્ર એ જ ક્ષેત્રો સપાટી પર લાવે છે જેના વિશે તેને ખબર હોય. એકવાર તમે infer કરેલું interface import કરો, એટલે ડોટ ટાઇપ કરતાં જ autocomplete કામ કરે છે — response પર કોઈ `as any` cast નહીં અને રિપો આખામાં હતાશ કરી મૂકતી grep નહીં.
  • જો તમે OpenAPI spec લખવા જઈ રહ્યા હો, તો infer કરેલું interface એ response schema નો ઝડપી પ્રથમ ડ્રાફ્ટ છે. તમને હજુ પણ હાથે લખેલા ઉદાહરણો અને મર્યાદાઓ જોઈશે, પણ પ્રોપર્ટીના નામ અને પ્રકારો પહેલેથી જ સાચા હોય છે.

સામાન્ય applications

Inference ત્યારે સૌથી વધુ મદદ કરે છે જ્યારે વાસ્તવિક payload હાજર હોય પણ schema ન હોય.

  • હેન્ડલર લખતા પહેલાં Stripe, GitHub, અથવા Twilio ના ત્રીજા-પક્ષના webhook payloads ને ટાઇપ કરવા.
  • આંતરિક REST API માટે પ્રકારો bootstrap કરવા જેથી જે દિવસે backend તૈયાર થાય તે જ દિવસે ફ્રન્ટએન્ડ ટીમ તેની સામે કોડિંગ શરૂ કરી શકે.
  • અવલોકન કરેલા API response પરથી Zod, io-ts, અથવા Valibot schema માટે એક શરૂઆતનો બિંદુ generate કરવો.

Output?

એક નમૂના JSON દસ્તાવેજ અને એક root નામ આપવામાં આવે, ત્યારે generator interfaces નું એક ટ્રી ઉત્પન્ન કરે છે, દરેક નેસ્ટેડ ઑબ્જેક્ટ માટે એક. root નામ User સાથે નીચે આપેલા ઇનપુટ માટે:

{"id":1,"name":"Alice","tags":["a","b"],"address":{"city":"Paris"}} ને root નામ User સાથે પેસ્ટ કરો અને generator ઉત્પન્ન કરે છે:

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

export interface Address {
  city: string;
}
નોંધો કે address ને તેના પોતાના નામવાળા interface માં promote કરવામાં આવ્યું — એ જ dependency-ક્રમમાં ગોઠવેલું આઉટપુટ છે. એ જ JSON ને બદલે type ઘોષણા શૈલી સાથે export type User = {...} emit કરશે; readonly ટૉગલ ચાલુ હોય ત્યારે દરેક પ્રોપર્ટીને readonly modifier મળે છે.

Generator options

Declaration style

interface (standard TypeScript object shapes) type (mapped types, conditional types, intersections). Both identical runtime; coding-style preference.

Optional nullable fields

Sampled value null, field type T | null. Option on ? modifier field optional — API key omit instead null.

Readonly modifier

Every property declaration readonly prepend, immutable data model. Redux state slices, frozen API responses, accidental mutation compiler flag.

શું આ nested objects અને arrays ને આધાર આપે છે?

હા. દરેક નેસ્ટેડ ઑબ્જેક્ટ તેની પેરન્ટ પ્રોપર્ટી કી પરથી તારવેલું એક નામવાળું interface બની જાય છે, અને ઍરે તેમની સામગ્રી પરથી item પ્રકાર infer કરે છે. ઑબ્જેક્ટના ઍરેને દરેક ઑબ્જેક્ટ shape દીઠ એક interface મળે છે, જ્યાં shapes અસંમત હોય ત્યાં union પ્રકારો સાથે.

વૈકલ્પિક ક્ષેત્રોનું અનુમાન કેવી રીતે થાય છે?

“Mark null-able fields optional” ટૉગલ ચાલુ કરો અને કોઈપણ ક્ષેત્ર જેનું નમૂના લીધેલું મૂલ્ય null હોય તેને કી પર ? modifier વત્તા પ્રકારમાં | null મળે છે. ટૉગલ વગર, ક્ષેત્ર આવશ્યક રહે છે અને પ્રકાર માત્ર T | null હોય છે.

શું આ discriminated unions ને આધાર આપે છે?

પાયાના union પ્રકારો ત્યારે બહાર આવે છે જ્યારે કોઈ ઍરે મિશ્ર-shape વાળા items રાખે અથવા જ્યારે કોઈ ક્ષેત્ર મૂલ્ય અને null બંને વહન કરે. પૂર્ણ discriminated-union inference (type અથવા kind ને ટૅગ તરીકે પસંદ કરવું અને variants ને વિભાજિત કરવા) માટે અનેક નમૂનાઓ જોઈએ — એ આયોજિત છે પણ આજના build માં નથી.

શું હું એકથી વધુ JSON નમૂનાઓ પરથી પ્રકારોનું અનુમાન કરી શકું?

હજુ નહીં — આજનું inferrer એક સમયે એક નમૂનો વાંચે છે. જો તમારી પાસે બે payloads હોય જે એક interface વહેંચવા જોઈએ (ધારો કે, એક list endpoint અને એક single-item endpoint), તો વ્યવહારુ ઉપાય એ છે કે તેમને એક ઍરેમાં merge કરો, તેમાંથી generate કરો, અને પછી પરિણામી union પ્રકારોને નામ બદલો. Multi-sample inference roadmap પર છે કારણ કે એ જ એકમાત્ર રીત છે એવા ક્ષેત્રો ઓળખવાની જે એક response માં હાજર હોય અને બીજામાં ગેરહાજર.

Payload paste, root name, interfaces copy. Browser pipeline, unreleased API signed webhook body machine.