JSON-to-TypeScript inference എങ്ങനെ പ്രവർത്തിക്കുന്നു
Inference parsed JSON tree-ൽ ഒരൊറ്റ pass ആണ്. ടൂൾ ഓരോ value-ഉം read ചെയ്ത് TypeScript type pick ചെയ്ത്, find ചെയ്ത ഓരോ object-ന്ഉം ഒരൊറ്റ interface write out ചെയ്യുന്നു.
- Browser native parser ഉൾക്കൊണ്ട് JSON sample parse ചെയ്ത് malformed input line/column hint ഉൾക്കൊണ്ട് reject ചെയ്യൽ.
- ഓരോ value-ഉം TypeScript type sniff ചെയ്യൽ —
string,number,boolean,null, array, nested object. - ഓരോ nested object-ഉം parent property key-ൽ നിന്ന് derive ചെയ്ത interface name നൽകൽ (
user.address→Addressinterface). - ഓരോ array-ലുടനീളം item types merge ചെയ്ത്,
{id: 1},{id: 2, label: "x"}ഉൾക്കൊള്ളുന്ന list ശരിയായ optional fields ഉൾക്കൊള്ളുന്ന union produce ചെയ്യൽ. - Options apply ചെയ്ത് (interface vs. type, readonly, optional-nullable) forward references ഇല്ലാതെ file compile ആകുന്ന വിധം dependency order-ൽ declarations emit ചെയ്യൽ.
JSON-ൽ നിന്ന് TypeScript types generate ചെയ്യേണ്ടത് എന്തുകൊണ്ട്?
- Response type written down ആണെങ്കിൽ shape bugs compile time-ൽ catchable. Real payload-ൽ നിന്ന് interface infer ചെയ്ത് ഭൂരിഭാഗം write ചെയ്യുന്നു, `strict` mode docs miss ആയ field catch ചെയ്യും.
- Zod, io-ts runtime validator ഉൾക്കൊണ്ട് inferred interfaces pair ചെയ്ത് ഒരേ shape-ന് രണ്ട് ജോലി: development-ൽ editor autocomplete, production unexpected-ൽ edge-ൽ 400.
- TypeScript language server known fields മാത്രം surface ചെയ്യുന്നു. Inferred interface import ചെയ്ത് dot type ചെയ്ത് autocomplete — repo-ൽ `as any` cast, frustrated grep ഇല്ല.
- OpenAPI spec write ചെയ്യാൻ പോകുന്നെങ്കിൽ, inferred interface response schema-ന്റെ fast first draft ആണ്. Property names, types ഇതിനകം correct.
സാധാരണ ഉപയോഗങ്ങൾ
Real payload ഉള്ളിടത്ത് schema ഇല്ലാത്തപ്പോൾ inference ഏറ്റവും ഉപകരിക്കുന്നു.
- Stripe, GitHub, Twilio-ൽ നിന്ന് third-party webhook payloads handler write ചെയ്യുന്നതിന് മുൻപ് type ചെയ്യൽ.
- Internal REST API-ന് types bootstrap ചെയ്ത് backend land ആകുന്ന ഒരേ ദിവസം frontend team code ആരംഭിക്കൽ.
- Observed API response-ൽ നിന്ന് Zod, io-ts, Valibot schema-ന് starting point generate ചെയ്യൽ.
ഔട്ട്പുട്ട് എങ്ങനെ കാണുന്നു?
Sample JSON document, root name ഉൾക്കൊണ്ട് generator ഒരൊറ്റ tree of interfaces produce ചെയ്യുന്നു, ഓരോ nested object-ഉം ഒന്ന്. Root name User ഉൾക്കൊണ്ട് ഇൻപുട്ടിന്:
{"id":1,"name":"Alice","tags":["a","b"],"address":{"city":"Paris"}} root name User ഉൾക്കൊണ്ട് paste ചെയ്ത് generator produce ചെയ്യുന്നു:
export interface User {
id: number;
name: string;
tags: string[];
address: Address;
}
export interface Address {
city: string;
}
address സ്വന്തം named interface-ലേക്ക് promote ആയതു ശ്രദ്ധിക്കുക — dependency-ordered output. type declaration style-ൽ export type User = {...}; readonly toggle ഓണിൽ ഓരോ property-ഉം readonly modifier.
Generator ഓപ്ഷനുകൾ
ഡിക്ലറേഷൻ ശൈലി
Object shapes-ന് interface (standard TypeScript idiom) അല്ലെങ്കിൽ type (mapped types, conditional types, intersections later-ൽ ആവശ്യമെങ്കിൽ) pick ചെയ്യൽ. ഇരുവരും identical runtime behaviour; coding-style preference.
ഐച്ഛിക nullable ഫീൽഡുകൾ
Sampled value null ആണെങ്കിൽ field-ന്റെ type T | null ആകുന്നു. ഈ option ഓണ്, ? modifier add ചെയ്ത് TypeScript side-ൽ field optional — API null return ചെയ്യുന്നതിന് പകരം key omit ചെയ്യുമ്പോൾ useful.
Readonly മോഡിഫയർ
ഓരോ property declaration-ഉം readonly prepend ചെയ്ത് emitted interface immutable data model match ചെയ്യുന്നു. Redux state slices, frozen API responses, accidental mutation compiler flag ആക്കണമെങ്കിൽ.
Nested objects, arrays support ഉണ്ടോ?
ഉണ്ട്. ഓരോ nested object-ഉം parent property key-ൽ നിന്ന് derive ചെയ്ത named interface ആകുന്നു, arrays contents-ൽ നിന്ന് item type infer ചെയ്യുന്നു. Objects-ന്റെ arrays shape per object interface, shapes disagree ആയ ഇടത്ത് union types.
Optional fields infer ചെയ്യുന്നത് എങ്ങനെ?
"Mark null-able fields optional" toggle ഓണ്, sampled value null ആയ ഏത് field-ഉം key-ൽ ? modifier, type-ൽ | null ലഭിക്കുന്നു. Toggle ഇല്ലാതെ field required, type T | null മാത്രം.
Discriminated unions support ഉണ്ടോ?
Array mixed-shape items hold ചെയ്യുമ്പോഴോ field value, null ഇരുവരും carry ചെയ്യുമ്പോഴോ basic union types ഉണ്ടാകുന്നു. Full discriminated-union inference (tag ആക്കി type/kind pick ചെയ്ത് variants split ചെയ്യൽ) multiple samples ആവശ്യമാണ് — planned, current build-ൽ ഇല്ല.
Multiple JSON samples-ൽ നിന്ന് types infer ചെയ്യാൻ കഴിയുമോ?
ഇതുവരെ ഇല്ല — current inferrer ഒരേ ഒരു sample ഒരേ സമയം read ചെയ്യുന്നു. ഒരേ interface share ചെയ്യേണ്ട രണ്ട് payloads ഉണ്ടെങ്കിൽ practical workaround: ഒരൊറ്റ array-ൽ merge ചെയ്ത്, generate ചെയ്ത്, resulting union types rename ചെയ്യൽ. Multi-sample inference roadmap-ൽ ഉണ്ട്.
Payload paste ചെയ്ത്, root name ചെയ്ത്, interfaces copy ചെയ്തോളൂ. Pipeline ബ്രൗസറിൽ run ആകുന്നതിനാൽ unreleased API, signed webhook body machine-ൽ നിൽക്കുന്നു.