[types] body.json() return type
See original GitHub issueCurrently 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:
- Created a year ago
- Comments:7 (4 by maintainers)
Top 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 >
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 Free
Top 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
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
asunknown
to see weak points in code whereany
is used without casting to some type.Here’s also related issue in TS: https://github.com/microsoft/TypeScript/issues/26188
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
definesbody.json()
to be returningPromise<any>
, as well: https://github.com/microsoft/TypeScript/blob/298b3a432ca099330108ed7bb18293ae79693672/lib/lib.dom.d.ts#L2448