{"id":"ed236a1ffbbbf70e","repo":"sindresorhus/is","slug":"expected-value-which-is-t-received-value-of-typ","errorCode":null,"errorMessage":"Expected value which is `T`, received value of type `${is(instance)}`.","messagePattern":"Expected value which is `T`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1550,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('Class', value));\n\t}\n}\n\nexport function assertDataView(value: unknown, message?: string): asserts value is DataView {\n\tif (!isDataView(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('DataView', value));\n\t}\n}\n\nexport function assertDate(value: unknown, message?: string): asserts value is Date {\n\tif (!isDate(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Date', value));\n\t}\n}\n\nexport function assertDirectInstanceOf<T>(instance: unknown, class_: Class<T>, message?: string): asserts instance is T {\n\tif (!isDirectInstanceOf(instance, class_)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('T', instance));\n\t}\n}\n\nexport function assertEmptyArray(value: unknown, message?: string): asserts value is never[] {\n\tif (!isEmptyArray(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('empty array', value));\n\t}\n}\n\nexport function assertEmptyMap(value: unknown, message?: string): asserts value is Map<never, never> {\n\tif (!isEmptyMap(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('empty map', value));\n\t}\n}\n\nexport function assertEmptyObject<Key extends keyof any = string>(value: unknown, message?: string): asserts value is Record<Key, never> {\n\tif (!isEmptyObject(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('empty object', value));","sourceCodeStart":1532,"sourceCodeEnd":1568,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1532-L1568","documentation":"Thrown by `assertDirectInstanceOf(instance, class_)` when `isDirectInstanceOf` fails — the value's prototype must be exactly `class_.prototype` (`Object.getPrototypeOf(instance) === class_.prototype`), not an instance of a subclass or from another realm. The message's literal `T` is the generic parameter name, so the expected-type text is unfortunately non-descriptive; pass a custom `message` for clarity.","triggerScenarios":"Passing a subclass instance (direct check rejects inheritance, unlike `instanceof`); passing an object created with `Object.create` of a different prototype; instances crossing realm boundaries (iframe, vm module, worker) where the class identity differs; duplicate copies of a library creating two distinct class objects.","commonSituations":"npm dependency duplication (two versions of a package means two `Error`/model class identities); Jest/vitest module isolation creating fresh class copies per test file; extending a class and expecting the parent's direct check to accept it; Node `vm` or JSDOM realms.","solutions":["If subclass instances should be accepted, use a plain `instanceof` check or `assert.instanceOf`-style logic instead of the direct check.","Deduplicate the package providing the class (`npm dedupe`, check `npm ls <pkg>`) so both sides share one class identity.","Pass a custom message so failures name the real class: `assertDirectInstanceOf(x, Foo, 'Expected direct Foo instance')`.","Ensure the value isn't crossing a realm boundary; reconstruct it locally if it is."],"exampleFix":"// before — subclass rejected by direct check\nassert.directInstanceOf(new AdminUser(), User);\n// after — accept subclasses\nif (!(user instanceof User)) throw new TypeError('Expected User');","handlingStrategy":"type-guard","validationCode":"if (!(instance instanceof ExpectedClass)) {\n  // wrong class or cross-module duplicate — construct via the same module's export\n}","typeGuard":"function isInstanceOf<T>(instance: unknown, ctor: new (...args: any[]) => T): instance is T {\n  return instance instanceof ctor;\n}","tryCatchPattern":"try {\n  assert.directInstanceOf(instance, ExpectedClass);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // note: directInstanceOf requires the EXACT class — subclasses fail; duplicate package versions also break identity\n  } else throw error;\n}","preventionTips":["directInstanceOf checks the immediate prototype — a subclass instance fails; use plain instanceof checks if subclasses should pass","Duplicate copies of a package in node_modules create distinct class identities — dedupe so both sides import the same module","instanceof fails across realms (workers, vm, iframes) — pass plain data across boundaries and reconstruct locally"],"tags":["type-assertion","instanceof","prototype","realm","dependency-duplication"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}