另一个“TypeError:XXX.default不是构造函数”错误

问题描述 投票:0回答:1

我在一个 TypeScript 文件中定义了一个类,我想在另一个 TypeScript 文件中实例化它,但收到错误消息

TypeError: skillService_1.default is not a constructor

我阅读了许多相关的 Stack Overflow 问题,但没有一个能解决我的问题。我怀疑存在循环依赖,但我用 madge 检查了我的代码,但没有任何循环依赖。在许多帖子中也推荐了

default export
,但这并不能解决我的问题。

skillService.ts

import ProgLangModel from '../models/progLang/progLangModel'
import { GitLabDiff, getGitLabFolders } from './versionControlService'
import isInPackageExclusionList from  '../config/packageExclusion'
import { Gitlab } from '@gitbeaker/core'
const similarity = require('../utils/stringSimilarity');

export default class TreeNode {

    private _name: string | null
    private _parent: TreeNode | null
    private _score: number | null
    private _progLangId: number | null
    private _children: TreeNode[]

    constructor(_name: string | null, _parent: TreeNode | null, _score: number | null, _progLangId: number | null) {
        this._name = _name;
        this._score = _score;
        this._children = [];
        this._parent = _parent;
        if (_parent)
            this._parent!.addToChildren(this)
        this._progLangId = _progLangId;
    }

    hasPackage(packageName: string): boolean {
    return this._children?.find(e => e._name === packageName) ? true : false;
    }
    ...

extractionService.ts

import TreeNode, { populateSkillsFromContent, calculateCumulatedScore } from './skillService'
import log from '../models/progressLog/progressLogDataService'
import saveExtraction from '../models/extraction/extractionDataService'
import { getGitLabProjects, getGitLabCommits, getGitLabDiffList, getGitLabContentByCommitId, GitLabDiff } from './versionControlService'
import updateSkillTree from '../models/skill/skillDataService'
import saveProject from '../models/project/projectDataService'
import createGitLabApi from '../config/initGitLabApi'
import { Gitlab } from '@gitbeaker/core'
import { GitLabProjectType } from './versionControlService'

const start = async (repoId: number, branches: {}, path: string, progLangs: number[]) => {
    ...
    let skillTree: TreeNode = new TreeNode(null, null, null, null)
    ...

我还没有尝试我的应用程序(我处于早期阶段),当我用 Jest 运行测试时出现错误:

RUNS  services/extractionService.test.ts
Waiting for the debugger to disconnect...
/home/ubuntu/work/skillhunter/server/services/extractionService.ts:2415
      (cov_2dfwuq4qdi().s[55]++, new skillService_1.default(null, null, null, null));
                             ^

TypeError: skillService_1.default is not a constructor
    at /home/ubuntu/work/skillhunter/server/services/extractionService.ts:2415:34
    at Generator.next (<anonymous>)
    at fulfilled (/home/ubuntu/work/skillhunter/server/services/extractionService.ts:2291:24)

Node.js v20.9.0

在测试中我没有提及

TreeNode

我做错了什么?

javascript typescript
1个回答
0
投票

我的问题是我的

TreeNode
类与使用该类的其他函数位于一个文件中。一旦类被移动到一个单独的文件中,这个错误就消失了。

© www.soinside.com 2019 - 2024. All rights reserved.