英文:
instanceof returns false on a true condition
问题
// discord-debug
import { Client } from 'discord.js';
export class Debugger {
constructor(public client: Client) {
console.log(client instanceof Client); // logs false
}
}
// main.ts
import { Client } from 'discord.js';
import { Debugger } from 'discord-debug';
const client = new Client({ intents: [...] }); // intents can be anything
console.log(client instanceof Client) // logs true
const debug = new Debugger(client);
if I move discord-debug into the root folder of "main.ts", it logs true
I'm publishing my npm package, discord-debug, and while testing it locally, it works well. But when I install it via npm
or an external folder, it throws the following error:
throw new TypeError('`client` must be a discord.js Client instance');
^
TypeError: `client` must be a discord.js Client instance
Which is defined in this line
Since the error doesn't cover much of the issue, it really is a hard task figuring it out and fixing it.
UPDATE
// package.json in "discord-debug"
"peerDependencies": {
"discord.js": "^14.11.0"
},
And when I use the package, I get the following error:
node_modules/discord-debug/dist/index.d.ts:1:49 - error TS2307: Cannot find module 'discord.js' or its corresponding type declarations.
1 import { Client, Message, type Snowflake } from 'discord.js';
~~~~~~~~~~~~
node_modules/discord-debug/dist/lib/Command.d.ts:1:87 - error TS2307: Cannot find module 'discord.js' or its corresponding type declarations.
1 import { ChatInputCommandInteraction, Collection, Message, SlashCommandBuilder } from 'discord.js';
NOTES
- When I use
console.log(client)
in discord-debug, it logs "undefined" - The version of
peerDependency
discord.js of discord-debug is the same as thedependency
discord.js of the project being worked on
FILE STRUCTURE
discord-debug/
├── dist/
│ ├── commands/
│ ├── lib/
│ └── index.js
├── src/
│ ├── commands/
│ ├── lib/
│ └── index.ts
└── tests/
└── main.js
my-project/
└── src/
└── main.ts
-
requiring
discord-debug
via../dist
intests/main.js
works. -
requiring
discord-debug
in my-project vianpm i ../discord-debug
throws the error.
<details>
<summary>英文:</summary>
**CONTEXT**
```js
// discord-debug
import { Client } from 'discord.js';
export class Debugger {
constructor(public client: Client) {
console.log(client instanceof Client); // logs false
}
}
// main.ts
import { Client } from 'discord.js';
import { Debugger } from 'discord-debug';
const client = new Client({ intents: [...] }); // intents can be anything
console.log(client instanceof Client) // logs true
const debug = new Debugger(client);
if I move discord-debug into the root folder of "main.ts
", it logs true
I'm publishing my npm package, discord-debug, and while testing it locally, it works well. But when I install it via npm
or an external folder, it throws the following error:
throw new TypeError('`client` must be a discord.js Client instance');
^
TypeError: `client` must be a discord.js Client instance
Which is defined in this line
Since the error doesn't cover much of the issue, it really is a hard task figuring it out and fixing it.
UPDATE
// package.json in "discord-debug"
"peerDependencies": {
"discord.js": "^14.11.0"
},
And when I use the package, I get the following error:
node_modules/discord-debug/dist/index.d.ts:1:49 - error TS2307: Cannot find module 'discord.js' or its corresponding type declarations.
1 import { Client, Message, type Snowflake } from 'discord.js';
~~~~~~~~~~~~
node_modules/discord-debug/dist/lib/Command.d.ts:1:87 - error TS2307: Cannot find module 'discord.js' or its corresponding type declarations.
1 import { ChatInputCommandInteraction, Collection, Message, SlashCommandBuilder } from 'discord.js';
NOTES
- When I use
console.log(client)
in discord-debug, it logs "undefined" - The version of
peerDependency
discord.js of discord-debug is the same as thedependency
discord.js of the project being worked on
FILE STRUCTURE
discord-debug/
├── dist/
│ ├── commands/
│ ├── lib/
│ └── index.js
├── src/
│ ├── commands/
│ ├── lib/
│ └── index.ts
└── tests/
└── main.js
my-project/
└── src/
└── main.ts
-
requiring
discord-debug
via../dist
intests/main.js
works. -
requiring
discord-debug
in my-project vianpm i ../discord-debug
throws the error.
答案1
得分: 1
假设这两个目录有单独的 node_modules
文件夹,那么 instanceof
返回 false
是正确的,因为 instanceof
通过引用相等性检查类是否在实例的原型链中的某个位置。如果消费代码恰好使用不同版本的 discord
库,你会在你的库的实际使用中遇到类似的问题。因此,通常最好让库代码对依赖外部库的任何必要类型检查使用鸭子类型(duck typing),或者仅依赖 TypeScript + 文档,而不在运行时进行检查。
英文:
Assuming the two directories have separate node_modules
folders, it's correct that instanceof
returns false
, because instanceof
checks that the class is somewhere in the instance's prototype chain via reference equality. You'll hit similar issues with real-world usage of your library if the consuming code happens to use a different version of the discord
library. For that reason, it's typically better for library code to use duck typing for any necessary type checks that rely on external dependencies (or simply rely on TypeScript + documentation and don't check at runtime at all).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论