首页 > 解决方案 > 无法命名来自外部模块的类型

问题描述

我收到一个非常奇怪的打字稿错误,我不知道为什么会这样:

我有一个函数“cleanArgs”可以正常工作并且输入正确。但是由于某种原因,当我在类方法中使用此函数时,该方法出现错误:

导出类的公共方法的返回类型已经或正在使用来自外部模块“./project/file.ts”的名称“CreatePlanGroupArgs”,但不能命名.ts(4053)

导出类的公共方法的返回类型已经或正在使用来自外部模块“./project/file.ts”的名称“ReadPlanGroupArgs”,但不能命名为.ts(4053)

import { CreatePlanArgs } from 'createPlan/CreatePlanArgs'
import { Helper } from 'helper/Helper'

// this is typed properly

const cleanArgs = (createPlanArgs: CreatePlanArgs) => {
  const { planGroup, ...plan } = Helper.sanitize(createPlanArgs)
  const create = Helper.sanitize(planGroup.create)
  const find = Helper.sanitize(planGroup.find)
  const pg = { create, find }
  return { planGroup: pg, plan }
}

export class Process {
  constructor (
    private readonly args: {
      createPlanArgs: CreatePlanArgs;
    }
  ) {}

  cleanArgs () { // < the error is on this line
    return cleanArgs(this.args.createPlanArgs)
  }
}

该类CreatePlanArgs看起来像这样:

@InputType()
class CreatePlanGroupArgs {
  @Field(() => String)
  name: string
}

@InputType()
class ReadPlanGroupArgs {
  @Field(() => String)
  uuid: string
}

@InputType()
class SelectPlanGroupArgs {
  @Field(() => ReadPlanGroupArgs, { nullable: true })
  find: ReadPlanGroupArgs

  @Field(() => CreatePlanGroupArgs, { nullable: true })
  create: CreatePlanGroupArgs
}

@ArgsType()
export class CreatePlanArgs {
  @Field(() => SelectPlanGroupArgs)
  planGroup: SelectPlanGroupArgs
}

标签: typescript

解决方案


出于某种原因,我不得不将每个类CreatePlanArgs从新文件中拆分出来。


推荐阅读