Convert JSON to TypeScript types
Turn a JSON sample, such as an API response, into TypeScript interfaces or types: nested objects named, optional keys and null unions inferred.
Turn a JSON sample, such as an API response, into TypeScript interfaces or types: nested objects named, optional keys and null unions inferred.
Types are inferred from this sample only: dates stay strings, and a key absent from every item cannot be guessed.
Runs entirely in your browser: nothing you type is sent or stored.
any, so a typo in a property name only shows up in production. Paste a real response here and you get the interfaces that describe it, ready to copy into your project: autocompletion, and compile errors as soon as you use a property that does not exist.
HttpClient and the response is typed in the rest of the code.
getUser(id: number): Observable<Root> {
return this.http.get<Root>(`/api/users/${id}`);
}home_address gives HomeAddress.items gives Item) and merged: a key missing from some items becomes optional, coupon?: string.null gives a union: string | null. An array of mixed values gives (string | number)[].unknown[], an empty object Record<string, unknown>, as there is nothing to infer from.Item, Item2).first-name, are quoted.interface is the usual choice for API data: it can be extended (extends) and its errors are easier to read. type is needed to name a union or an array, which is why a JSON document that is an array or a single value always gets a type at the top.
readonly prevents the code from changing a property by mistake, handy for data received from a server or stored in a signal.
string since JSON has no date type: convert them yourself where you need a Date.null but is not in this response, cannot be guessed. Paste a response with several varied items for a better result.number, and a string that can only take a few values (a status, a currency) is not turned into a union of literals: narrow it by hand, 'paid' | 'pending'.