You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入TypeScript类型时出现Cannot use namespace as a type错误

无法正确导入NPM包中的TypeScript接口问题

我开发了一个带TS类型的公共NPM包,用于跨代码库共享。包的src/index.ts导出了CreateCustomer接口,构建后发布到本地NPM仓库。下游项目用import {CreateCustomer} from '@company/common'导入并作为类型使用时,出现错误:Cannot use namespace 'CreateCustomer' as a type.ts(2709);但把导入路径改成@company/common/后错误消失,找不到接口被额外模块/命名空间包裹的原因,求解决办法。


相关代码与配置

1. 包源码 src/index.ts

export interface CreateCustomer {
    "email": string,
    "firstname": string,
    "lastname": string,
    "entityid"?: string,
    "stage": string
};

2. tsconfig.json

{
  "compilerOptions": {
      "allowUnreachableCode": false,
      "esModuleInterop": true,
      "target": "ES2021",
      "module": "commonjs",
      "lib": ["es2021", "DOM"],
      "resolveJsonModule": true,
      "noEmitOnError": true,
      "declaration": true,
      "strict": true,
      "noImplicitAny": true,
      "strictNullChecks": true,
      "noImplicitThis": true,
      "alwaysStrict": true,
      "noUnusedLocals": true,
      "noUnusedParameters": true,
      "noImplicitReturns": true,
      "noFallthroughCasesInSwitch": true,
      "inlineSourceMap": true,
      "inlineSources": true,
      "experimentalDecorators": true,
      "strictPropertyInitialization": true,
      "skipLibCheck": true,
      "stripInternal": true,
      "outDir": "./dist",
      "rootDir": "./src",
      "typeRoots": ["./node_modules/@types"]
  },
  "include": ["**/src/*.ts"]
}

3. package.json

{
  "name": "@company/common",
  "version": "1.0.7",
  "description": "common library ",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "scripts": {"build": "tsc"},
  "devDependencies": {
    "@aws-lambda-powertools/logger": "^1.1.0",
    "@aws-lambda-powertools/tracer": "^1.1.0",
    "@aws-sdk/client-secrets-manager": "^3.50.0",
    "@types/aws-lambda": "^8.10.108",
    "axios": "^1.1.3",
    "netsuite-rest": "^1.1.0",
    "nsrestlet": "^2.0.1"
  },
  "files": ["dist"],
  "keywords": [],
  "author": "",
  "license": "ISC"
}

4. 文件结构

company
│
└───common
│   │   package.json
│   │   readme.md
│   │
│   └───dist
│       │   index.js
│       │   index.d.ts

排查与解决步骤

1. 检查生成的声明文件

先看构建后dist/index.d.ts的内容,正常情况下它应该直接导出接口:

export interface CreateCustomer {
    email: string;
    firstname: string;
    lastname: string;
    entityid?: string;
    stage: string;
}

如果文件里出现declare namespace CreateCustomer这类代码,说明编译时混入了额外内容,需要排查配置或文件命名问题。

2. 修正TS配置的include规则

当前include规则["**/src/*.ts"]可能会匹配到项目外的其他src目录,改成明确指向当前包的src:

"include": ["./src/**/*.ts"]

3. 确认package.json入口配置

确保main和types字段指向的路径正确,且dist目录下确实存在对应文件。如果后续切换到ES模块,可以添加module字段指向ES版本入口,但当前用commonjs只需保持main配置不变。

4. 清理缓存并重新发布

  • 删除下游项目node_modules/@company/common目录,清除TS缓存(删除node_modules/.cache或重启TS语言服务)
  • 在公共包目录重新执行npm run build,再发布到本地仓库
  • 下游项目重新安装包后测试导入

5. 检查命名冲突

确保项目中没有其他同名的CreateCustomer文件或目录,避免TS解析时混淆模块。


内容的提问来源于stack exchange,提问作者James L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 23:40:19