TypeScript · TS0013

Do not use instanceof for web platform values from another realm

Report instanceof checks against web platform and binary data classes, such as Response, Request, Blob, URL, ArrayBuffer and Uint8Array, on values that may come from another JavaScript realm or another copy of the class.
TS0013vite-doctor/typescriptboundarieswarnNo fix

Run this rule

pnpm vite-doctor . --extends auto,typescript/strict --rules typescript/boundaries/no-cross-realm-web-instanceof

Why it matters

Response, Request, Headers, Blob, URL, ArrayBuffer, Uint8Array and the other web platform classes have one constructor per realm, and often more than one per process: a second copy of undici such as Miniflare's, the Blob and URL of a Vitest DOM environment, a node:vm context or an iframe. A value built by any of them fails instanceof against this realm's class, so the check takes the wrong branch. This rule only reports values that crossed a boundary in this file: parameters typed unknown or any, results of calling a parameter, and module namespaces from import() or ssrLoadModule(). A parameter of a function that the file neither exports nor passes around is skipped when every call passes a value proven local, such as a literal or an object constructed with an unshadowed built-in. Catching an error does not establish its origin. JavaScript built-ins such as Error and Date are reported by typescript/boundaries/no-cross-realm-builtin-instanceof.

Use a realm-safe intrinsic or a tag shared by the implementations you support where applicable. A member check only narrows to the capabilities it verifies, not the full class. If you need full class semantics, validate or normalize the value at the integration boundary.

Example

Brand-check values from host callbacks

Before

export function toResponse(value: unknown): Response {
  if (value instanceof Response) return value
  return Response.json(value)
}

After

function isResponse(value: unknown): value is Response {
  return Object.prototype.toString.call(value) === '[object Response]'
}

export function toResponse(value: unknown): Response {
  if (isResponse(value)) return value
  return Response.json(value)
}
Copyright © 2026