instanceof 在条件为真时返回 false。

huangapple go评论81阅读模式
英文:

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 the dependency 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 in tests/main.js works.

  • requiring discord-debug in my-project via npm i ../discord-debug throws the error.


<details>
<summary>英文:</summary>

**CONTEXT**
```js
// discord-debug
import { Client } from &#39;discord.js&#39;;

export class Debugger {
    constructor(public client: Client) {
        console.log(client instanceof Client); // logs false
    }
}

// main.ts
import { Client } from &#39;discord.js&#39;;
import { Debugger } from &#39;discord-debug&#39;;

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(&#39;`client` must be a discord.js Client instance&#39;);
            ^

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 &quot;discord-debug&quot;
&quot;peerDependencies&quot;: {
    &quot;discord.js&quot;: &quot;^14.11.0&quot;
},

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 &#39;discord.js&#39; or its corresponding type declarations.

1 import { Client, Message, type Snowflake } from &#39;discord.js&#39;;
                                                  ~~~~~~~~~~~~

node_modules/discord-debug/dist/lib/Command.d.ts:1:87 - error TS2307: Cannot find module &#39;discord.js&#39; or its corresponding type declarations.

1 import { ChatInputCommandInteraction, Collection, Message, SlashCommandBuilder } from &#39;discord.js&#39;;

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 the dependency 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 in tests/main.js works.

  • requiring discord-debug in my-project via npm 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).

huangapple
  • 本文由 发表于 2023年6月26日 00:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定