Skip to content
Discussion options

You must be logged in to vote

the easiest way is to use the .json<T>() method:

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 responseType: 'json' and resolveBodyOnly: true:

const jobs = await got<Job[]>(url, {
	responseType: 'json',
	resolveBodyOnly: true
});

without resolveBodyOnly: true, you get the full response object and need to acces .body:

const response = await got<Job[]>(url, {
	responseType: 'json'
});
const jobs = response.body; // Job[]

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1890 on October 13, 2021 05:08.