1+ import { inspect } from './inspect' ;
2+
13/**
24 * A replacement for instanceof which includes an error warning when multi-realm
35 * constructors are detected.
@@ -10,16 +12,23 @@ export const instanceOf: (value: unknown, constructor: Constructor) => boolean =
1012 function instanceOf ( value : unknown , constructor : Constructor ) : boolean {
1113 return value instanceof constructor ;
1214 }
13- : function instanceOf ( value : any , constructor : Constructor ) : boolean {
15+ : function instanceOf ( value : unknown , constructor : Constructor ) : boolean {
1416 if ( value instanceof constructor ) {
1517 return true ;
1618 }
17- if ( value ) {
18- const valueClass = value . constructor ;
19- const className = constructor . name ;
20- if ( className && valueClass && valueClass . name === className ) {
19+ if ( typeof value === 'object' && value !== null ) {
20+ // Prefer Symbol.toStringTag since it is immune to minification.
21+ const className = constructor . prototype [ Symbol . toStringTag ] ;
22+ const valueClassName =
23+ // We still need to support constructor's name to detect conflicts with older versions of this library.
24+ Symbol . toStringTag in value
25+ ? // @ts -expect-error TS bug see, https://github.com/microsoft/TypeScript/issues/38009
26+ value [ Symbol . toStringTag ]
27+ : value . constructor ?. name ;
28+ if ( className === valueClassName ) {
29+ const stringifiedValue = inspect ( value ) ;
2130 throw new Error (
22- `Cannot use ${ className } "${ value } " from another module or realm.
31+ `Cannot use ${ className } "${ stringifiedValue } " from another module or realm.
2332
2433Ensure that there is only one instance of "graphql" in the node_modules
2534directory. If different versions of "graphql" are the dependencies of other
@@ -38,5 +47,7 @@ spurious results.`,
3847 } ;
3948
4049interface Constructor extends Function {
41- name : string ;
50+ prototype : {
51+ [ Symbol . toStringTag ] : string ;
52+ } ;
4253}
0 commit comments