TS0013Do not use instanceof for web platform values from another realm
Why it happens
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.
Fix
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)
}
Verify the fix
Run only this rule after editing so the report stays focused on the diagnostic you are closing:
pnpm vite-doctor . --extends auto,typescript/strict --rules typescript/boundaries/no-cross-realm-web-instanceof