Skip to content

Problematic PolymorphicRequest Type? #5768

Description

@lforst

Looking at #5744, the PolymorphicRequest type seems wrong or rather dangerous to me. IMO this type needs to be a union (|) instead of an intersection (&).

exporttypePolymorphicRequest=BaseRequest&
BrowserRequest&
NodeRequest&
ExpressRequest&
KoaRequest&
NextjsRequest;

Why?: With an intersection, we're creating a new type that none of the frameworks support. This might lead to us making wrong assumptions about what a request might look like. For example, let's say a ExpressRequest has a field someField: string, but none of the others have someField, TS will still allow us to access someField, even though none of the others have it. (Not the best example since all of the fields we define are optional - I know... but still valid)

What would I do?: Replace this type with a union and remove the optional fields from the framework-specific Request types (unless they're actually optional). That way we can apply proper type-safe type narrowing in all the places where we accept a PolymorphicRequest.

We probably don't have any actual errors because of this, but in its current state, this is very error-prone.

I am opening this in a separate issue so we can discuss this regardless of open PRs.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions