question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v3.0.0 `await res.json()` is `unknown`

See original GitHub issue

this is unusable in typescript. v3.0.0 types the return value of await res.json() as unknown which means “this is dangerous info, you have no idea what it is. don’t touch it to avoid runtime errors.” but the point of a fetch request is to get the information back. you have to touch it. the type should stay any.

Reproduction

Steps to reproduce the behavior:

  1. use node-fetch in a typescript project
  2. try to build
  3. see error

Expected behavior

for the build to succeed

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:29
  • Comments:36 (11 by maintainers)

github_iconTop GitHub Comments

36reactions
martijndehcommented, Sep 6, 2021

I don’t mind defaulting to unknown instead of any, but can we have something like this json<T = unknown>(): Promise<T>; instead?

29reactions
martijndehcommented, Sep 6, 2021

Aha, I see. Up to you of course. In my opinion it’s a bit more ergonomic to provide a type argument in the cases where you opt out of any runtime checks anyway — even if DefinitelyTyped thinks this is a common mistake. 😉

// Explicit type cast
const json1 = (await response.json()) as MyJson;
// Alternative type cast, not much better
const json2 = await (response.json() as Promise<MyJson>);
// Doesn't work anymore, 'unknown' is not assignable to type 'MyJson'
const json3: MyJson = await response.json();
// So pretty ;-)
const json4 = await response.json<MyJson>();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch API: Can 'await res.json()' fail after a completed request?
I am handling res to show the users the necessary error or success message using the flashResponseToUser(res) function. Since res.json() returns ...
Read more >
fetch() - Web APIs | MDN
The global fetch() method starts the process of fetching a resource from the network, returning a promise which is fulfilled once the response...
Read more >
Async Await JavaScript Tutorial – How to Wait for a Function to ...
Let's have a look. const getData = async () => { const response = await fetch("https://jsonplaceholder.typicode.com/todos/1") ...
Read more >
TypeScript - Fastify
Fastify offers two packages wrapping json-schema-to-ts and typebox : ... toUpperCase() }) done() } module.exports = fp(myPlugin, { fastify: '3.x', ...
Read more >
Object is of type 'unknown' Error in TypeScript | bobbyhadz
json file, the error variable will be typed as any . index.ts. Copied! async function ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found