Skip to content

fetch generic type violates "golden rule of generics" #543

@s-h-a-d-o-w

Description

@s-h-a-d-o-w

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions