v3.0.0 `await res.json()` is `unknown`
See original GitHub issuethis 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:
- use node-fetch in a typescript project
- try to build
- see error
Expected behavior
for the build to succeed
Issue Analytics
- State:
- Created 2 years ago
- Reactions:29
- Comments:36 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t mind defaulting to
unknown
instead ofany
, but can we have something like thisjson<T = unknown>(): Promise<T>;
instead?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. 😉