类型错误:不能在没有使用'new'的情况下调用类构造函数Client。

3

我正在开发一个基于ts-node(脚本模式)、typescript和discord.js的discord机器人框架。在测试核心客户端类时,我遇到了这个错误:


C:\Users\thedi\Documents\coding\JS-TS\discord-bot\src\struct\Client.ts:13
        super()
        ^
TypeError: Class constructor Client cannot be invoked without 'new'
    at new DigsClient (C:\Users\thedi\Documents\coding\JS-TS\discord-bot\src\struct\Client.ts:13:9)
    at Object.<anonymous> (C:\Users\thedi\Documents\coding\JS-TS\discord-bot\test\index.ts:6:16)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module.m._compile (C:\Users\thedi\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1056:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\thedi\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1059:12)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at main (C:\Users\thedi\AppData\Roaming\npm\node_modules\ts-node\src\bin.ts:198:14)

在StackOverflow上有很多回答相同的问题,大部分建议将tsconfig中的“target”值设置为“es6”或更高版本。但是这个解决方案对我无效。

这是我的代码:

// src/struct/Client.ts

import { Client, ClientOptions, Snowflake } from 'discord.js';
import * as discord from 'discord.js';

interface DigsClientOptions {
    ownerID?: Snowflake | Array<Snowflake>;
}

class DigsClient extends Client {
    public constructor(
        options?: DigsClientOptions,
        discordOptions?: ClientOptions
    ) {
        super();

        this.ownerID = options.ownerID;

        return this;
    }

    ownerID: Snowflake | Array<Snowflake>;
}

export { DigsClient, DigsClientOptions };


// test/index.ts

import { DigsClient } from '../src/index';
import { config } from 'dotenv';
config();
const token = process.env.TOKEN;

const client = new DigsClient({ ownerID: '585813838490894346' });

client.login(token);


//.tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "es6"
    },
    "exclude": ["node_modules"]
}


在使用 ts-nodets-node-script 时,会出现此错误,并且我知道它检测到了 tsconfig,因为我没有因使用 es6 模块而出现错误。

希望有人能帮忙!


尝试将 module 更改为 commonjstarget 是您想要使用的语法,而 module 是编译时想要使用的语法。 使用 commonjs,因为在 NodeJS 上通常使用 CommonJS。 - nouvist
@nouvist 看起来问题现在已经解决了,这真的是 ts-node 和 typescript 的编译器选项非常令人困惑的问题。不过你的答案确实有效,谢谢! - digs
1个回答

2
由于NodeJS遵循CommonJS模块系统,我认为原因是您应该将module设置为CommonJS。只要不会破坏现有的内容,您可以保留targetES6。因为据我所知,Node和现代浏览器都支持ES6语法。
但是,那么targetmodule之间有什么区别呢?target是您针对的整个语法,而module仅适用于您如何导入其他模块。直到14.x版本,NodeJS仅支持CommonJS。因此,推荐在NodeJS Typescript项目中使用CommonJS
就像我说的,这只是导入其他模块的方式。CommonJS是一种模块系统,这是一种允许您导入其他模块的系统,类似于HTML上的<script>标签。以下是一个CommonJS示例。
const foobar = require('./foobar.js');
foobar();

NodeJS使用这个模块系统,因为Javascript以前没有这样的系统。

但是,EcmaScript(Javascript标准)引入了ESModule,它是CommonJS的标准化替代品。ESModule是EcmaScript标准语法的一部分,其语法如下所示。

import foobar from './foobar.js';
foobar();

所以,您正在使用ESModule,但NodeJS只能理解CommonJS。这就是为什么我们需要将ES6编译成CommonJS,以便NodeJS可以理解。
但现在,NodeJS在13.x或14.x上支持ESModule(我不完全确定)。但是NodeJS仍然会默认使用CommonJS。因此,如果想要使用它,您需要告诉NodeJS您的包正在使用ESModule。
有几种选项可供选择。首先,使用“.mjs”扩展名,其次是在“package.json”中更改默认设置。
{
  "name": "my-epic-package",
  "version": "1.0.0",
  "type": "module", // here
  ...
}

所以,启用type模块后,您可以保留tsconfig.json文件来使用module。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接