Response body type conversion to interface #1891
-
|
I don't see any example on documentation to convert the response body to type I tried this and this But none of these supported Here is full code Checklist
|
Beta Was this translation helpful? Give feedback.
Answered by
sindresorhus
Oct 11, 2025
Replies: 1 comment
-
|
the easiest way is to use the interface Job {
id: string;
title: string;
}
const jobs = await got(url).json<Job[]>();
// jobs is typed as Job[]Alternatively, if you use the generic on the got call itself, you need to set both const jobs = await got<Job[]>(url, {
responseType: 'json',
resolveBodyOnly: true
});without const response = await got<Job[]>(url, {
responseType: 'json'
});
const jobs = response.body; // Job[] |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sindresorhus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the easiest way is to use the
.json<T>()method:Alternatively, if you use the generic on the got call itself, you need to set both
responseType: 'json'andresolveBodyOnly: true:without
resolveBodyOnly: true, you get the full response object and need to acces.body: