打字稿解析私有范围包的类型

问题描述

我在Azure Artifacts npm Feed中托管了一个程序包,范围为@company,例如添加我们使用yarn add @company/package的软件包。该软件包是一个打字稿项目,在编译时还包括打字稿文件

使用它时,无法解析公开的类型,并且编译失败。

构建示例:

  • 构建
    • index.js
    • index.d.ts

示例package.json

{
  "name": "@company/package","version": "0.0.5","module": "./build/index.js","types": "./build/index.d.ts","scripts": {
    "build": "rimraf ./build && tsc -p tsconfig.json --noEmit false","prepublish": "npm run build"
  },"devDependencies": {
    "typescript": "^3.9.7"
  },"files": [
    "build/"
  ]
}

在与package.json相同的级别上,有一个.npmrc文件一个tsconfig.json文件

.npmrc

@company:registry=https://company.pkgs.visualstudio.com/_packaging/company-npm/npm/registry/
                        
always-auth=true

tsconfig.json

{
  "compilerOptions": {
    "module": "esnext","target": "ES2015","lib": [
      "es2018","dom"
    ],"sourceMap": true,"allowJs": false,"jsx": "preserve","preserveConstEnums": false,"moduleResolution": "node","rootDir": "src","forceConsistentCasingInFileNames": true,"noImplicitReturns": true,"noImplicitThis": true,"noImplicitAny": true,"strictnullchecks": false,"suppressImplicitAnyIndexErrors": true,"removeComments": true,"skipLibCheck": true,"typeRoots": [
      "node_modules/@types"
    ],"noUnusedLocals": true,"resolveJsonModule": true,"esModuleInterop": true,"allowSyntheticDefaultImports": true,"strict": true,"outDir": "build/esm","declaration": true,"isolatedModules": true,"noEmit": true
  },"include": [
    "src/**/*"
  ],"exclude": [
    "node_modules/*","build","scripts","acceptance-tests","webpack","jest","src/setupTests.ts","src/__mocks__/Api.ts"
  ]
}

我们可以毫无问题地安装公共npmjs软件包和@types,并且@company/package也可以工作(@company文件夹出现在node_modules中,并包含软件包代码包括index.d.ts文件)。

当引用我们程序包中的代码(恰好是React组件)时,它显示错误,指出JSX element type 'Button' does not have any construct or call signatures,这是由于打字稿键入无法正确解析造成的。

我们尝试使用范围内的包更改项目的tsconfig.json,以便类型根也包括@company范围,例如

"typeRoots": [
      "node_modules/@types","node_modules/@company"
],

我们还尝试了各种类型的安装迭代(尚未手动发布,我只是认为它可能是npm publish的一部分)yarn add -D @company/@types/package @types/@company@package,但是两个命令都返回以下错误

yarn add v1.22.0
warning package.json: No license field
warning [email protected]: No license field
[1/4] Resolving packages...
error Command @R_404_4761@.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://[email protected]/types/company-react-components.git
Directory: /mnt/c/Users/user/source/repos/project/src/project.Ui
Output:
Host key verification @R_404_4761@.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

您如何正确引用作为范围包的一部分发布的类型?如果引用正确,为什么类型无法解析?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)