Skip to content

Fetch: new Response()/new Request() — instanceof always false and .constructor is undefined (no prototype link) #5433

Closed

Description

@proggeramlug

Summary

x instanceof Response (and instanceof Request) is always false, and x.constructor is undefined, for the native Fetch handles — even for an object freshly created with new Response(...). Field access (.status, .headers, …) works, but the prototype/constructor link is missing, so any code that discriminates a union with instanceof Response misbehaves.

Environment

  • perry 0.5.1189, main @ 0cc6ee14c, built from source
  • macOS arm64
  • No dependencies.

Repro

repro.ts:

const r = new Response('hi', { status: 200 });
console.log('typeof r             :', typeof r);
console.log('r instanceof Response:', r instanceof Response);
console.log('r.constructor?.name  :', (r as any).constructor?.name);
console.log('r.status             :', r.status);

const q = new Request('http://x/');
console.log('q instanceof Request :', q instanceof Request);
perry compile repro.ts -o repro && ./repro

Actual

typeof r             : object
r instanceof Response: false      <-- expected true
r.constructor?.name  : undefined  <-- expected "Response"
r.status             : 200
q instanceof Request : false      <-- expected true

Expected

new Response() instanceof Response === true and r.constructor === Response (matching Node/Bun/browsers), so instanceof-based discrimination works.

Impact

Very common idiom — e.g. a guard that returns either a value or a Response:

const auth = gate(c);            // AuthContext | Response
if (auth instanceof Response) return auth;   // always false under perry
use(auth.user.id);               // falls through, throws on the Response

This is the cause of a class of TypeError: Cannot read properties of undefined we hit across ~25 admin handlers in a Hono app. Workaround is duck-typing ('status' in x / a field-presence type guard). Related to the constructor-binding work in #2608/#2640.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions