私有范围包的 Typescript 解析类型

Typescript resolve types for private scoped package

我有一个包托管在 Azure Artifacts npm 源中,范围为@company,例如添加我们使用的包 yarn add @company/package。这个包是一个打字稿项目,编译时还包括类型文件。

使用时暴露的类型没有被解析,编译失败

构建示例:

示例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"
  ]
}

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

当从我们的包中引用代码(恰好是 React 组件)时,它显示错误 JSX element type 'Button' does not have any construct or call signatures,这是打字稿类型没有正确解析的结果。

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

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

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

yarn add v1.22.0
warning package.json: No license field
warning project@0.0.0: No license field
[1/4] Resolving packages...
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://git@github.com/types/company-react-components.git
Directory: /mnt/c/Users/user/source/repos/project/src/project.Ui
Output:
Host key verification failed.
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.

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

我知道这是一个老问题,但我只是花了半天时间用头撞墙才让它起作用。

您需要将包命名为 @myorg/mypackage 以使 npm 使用您在 .npmrc.

中定义的范围 @myorg:registry=https://pkgs.dev.azure.com/...

为了让 TypeScript 满意,并且不必手动调整 typeRoots,您可以安装包,并将其别名改回 @types/myorg__mypackage 命名:

npm i @types/myorg__mypackage@npm:@myorg/mypackage@latest --save-dev

您可能希望在自述文件中记录这一点,因为对于想要使用该软件包的可怜人来说,它一点也不直观。