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.

[types] body.json() return type

See original GitHub issue

Currently body.json() returns any type. It’s aligned to JSON.parse I guess. However from types perspective it’s not very good.

For example without additional linters and configs we could do the following:

const foo: any = 10; // We can assign anything to any
const bar: boolean = foo; // Any is assignable to anything 
foo.exec(); // Ok; anything goes with any

But if we have unknown type there it will be much safer and force end-users manually cast it to type that they expect.

const foo: unknown = 10; // We can assign anything to unknown, but
const bar: boolean = foo; // Error, we can't assign unknown to any other type without an explicit assertion
foo.exec(); // Error, we can't access a property and call it on unknown

On the other hand it will be a breaking change from typescript point of view and maybe someone have other point of view regarding type safety in this case.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SkeLLLacommented, Jul 21, 2022

Yes, changing the type will cause issues in existing projects, so if it’s considered to be changed it should go in semver-major. But in general those issues will allow users to improve their projects.

For example, sometimes we use a customized ts compiler that treats all any as unknown to see weak points in code where any is used without casting to some type.

Here’s also related issue in TS: https://github.com/microsoft/TypeScript/issues/26188

1reaction
meyfacommented, Jul 19, 2022

In my opinion it makes sense to mirror types from lib.dom.d.ts so that code written for both the browser and for Node doesn’t suffer from mismatched types. lib.dom.d.ts defines body.json() to be returning Promise<any>, as well: https://github.com/microsoft/TypeScript/blob/298b3a432ca099330108ed7bb18293ae79693672/lib/lib.dom.d.ts#L2448

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Data Types - W3Schools
Valid Data Types. In JSON, values must be one of the following data types: a string; a number; an object (JSON object); an...
Read more >
Response.json() - Web APIs - MDN Web Docs
It returns a promise which resolves with the result of parsing the body text as JSON .
Read more >
JSON | Data Types - GeeksforGeeks
JSON (JavaScript Object Notation) is most widely used data format for data ... Boolean: This data type can be either true or false....
Read more >
Return JSON object with TypeScript function - Stack Overflow
My question is: what return type should I use? Here is the function: function formaterDonnees(data: string) { // or (data: string): ...
Read more >
How do I return JSON in HTTP response? - ReqBin
JSON can represent four primitive types (strings, numbers, boolean values, and null) and two structured types (objects and arrays). JSON is used ...
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