sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

Property 'data' does not exist on type 'unknown' when using .post().json() #1548

Closed bluecomm42 closed 3 years ago

bluecomm42 commented 3 years ago

Describe the bug

Actual behavior

When using (await got.post().json()).data in TypeScript it gives the error Property 'data' does not exist on type 'unknown'. Note: This is a complier error, the code runs without issue, but the compile-time type checker isn't happy.

Expected behavior

I would expect that the code wouldn't error, without me having to resort to using // @ts-ignore or writing a custom interface to conform the returned object to.

Code to reproduce

// <snip>
const res = await got
  .post("<url>", { body: form, headers })
  .json();
return res.data; // <-- error is here

Checklist

szmarczak commented 3 years ago

The .json() function takes a template. Use .json<{data: type}>()

szmarczak commented 3 years ago

This is TypeScript - we are trying to be as strict as possible.