如何解决打字稿解析私有范围包的类型
我在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 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.
您如何正确引用作为范围包的一部分发布的类型?如果引用正确,为什么类型无法解析?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。