unjs / unjs/ofetch

fetch generic type violates "golden rule of generics"

Open
#543 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
5.4k
Forks
195
PR merge metrics
No merged PRs in 30d

Description

Describe the feature

As mentioned in Microsoft's TypeScript handbook and elaborated on by Dan Vanderkam, generics shouldn't be used as a replacement for type assertion.

And since the introduction of unknown, the recommended practice for transparency reasons has been to type return values that don't have an inherent type as unknown and potentially use type assertion (ideally, the return value of course undergoes a runtime check) to convert to a type that authors are reasonably confident that they will receive.

Which is why I would recommend changing the type accordingly, removing the option to specify a generic and returning unknown instead.

(I of course get that that's a significant DX change and there are some who don't care about these recommendations or reasons but I still think it's an important topic and major libraries should encourage good practices. Also, I'm pretty sure I've seen some major libraries do just this in the past. It's been a number of years, so I don't remember which one it was but something like axios or enzyme got rid of such a generic pattern way back when.)

Additional information
  • Would you be willing to help implement this feature?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names the fetch entry point but no file or test. Start by locating its generic return type and reviewing how the public API exposes it; done means removing the generic option and returning unknown instead, with the resulting TypeScript impact understood.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.