【问题标题】:Only default constructor accepted for a typescript class打字稿类只接受默认构造函数
【发布时间】:2016-12-28 23:46:34
【问题描述】:

我有 2 个打字稿文件。一个实现 SourceContext 类:

export class SourceContext {
    constuctor(sourceId: string) {
        this._sourceId = sourceId;
    }
...
}

在另一个文件中我想使用这个导出的类(两个文件都在我的 NodeJS 模块的同一个文件夹中):

import { SourceContext } from './SourceContext';

export class Service {
    public load(file: string) {
        var context = new SourceContext(file);
    }
}

但是我得到了错误:

文件:'file:///...../src/index.ts' 严重性:'Fehler' 消息: '提供的参数与调用目标的任何签名都不匹配。'

当我删除 file 参数时,不会出现错误。为什么它无法识别我定义的构造函数,我该如何解决?

【问题讨论】:

  • 您的代码似乎没问题,您不应该收到此错误。这是您要准确编译的内容吗?另外,你是如何建造这个的?这是什么错误信息?什么是“严重性:'Fehler'”?
  • 这个错误信息实际上来自 vscode 并且它被设置为德语本地化。服务性与这里无关。在我的 nodejs 模块的根目录中运行 tsc 时,我也会遇到完全相同的错误。

标签: typescript


【解决方案1】:

您的代码中有一个 DO'H。

export class SourceContext {
    constuctor(sourceId: string) {
        this._sourceId = sourceId;
    }
...
}

构造函数改为构造函数

【讨论】:

  • 天哪,,。 o O(该死的 js)
猜你喜欢
  • 2019-08-16
  • 2017-01-29
  • 2020-01-05
  • 2012-06-30
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多