1
+ import { inspect } from './inspect' ;
2
+
1
3
/**
2
4
* A replacement for instanceof which includes an error warning when multi-realm
3
5
* constructors are detected.
@@ -10,16 +12,23 @@ export const instanceOf: (value: unknown, constructor: Constructor) => boolean =
10
12
function instanceOf ( value : unknown , constructor : Constructor ) : boolean {
11
13
return value instanceof constructor ;
12
14
}
13
- : function instanceOf ( value : any , constructor : Constructor ) : boolean {
15
+ : function instanceOf ( value : unknown , constructor : Constructor ) : boolean {
14
16
if ( value instanceof constructor ) {
15
17
return true ;
16
18
}
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 ) ;
21
30
throw new Error (
22
- `Cannot use ${ className } "${ value } " from another module or realm.
31
+ `Cannot use ${ className } "${ stringifiedValue } " from another module or realm.
23
32
24
33
Ensure that there is only one instance of "graphql" in the node_modules
25
34
directory. If different versions of "graphql" are the dependencies of other
@@ -38,5 +47,7 @@ spurious results.`,
38
47
} ;
39
48
40
49
interface Constructor extends Function {
41
- name : string ;
50
+ prototype : {
51
+ [ Symbol . toStringTag ] : string ;
52
+ } ;
42
53
}
0 commit comments