首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >自定义Typescript NPM包不会生成类型

自定义Typescript NPM包不会生成类型
EN

Stack Overflow用户
提问于 2019-06-13 03:15:02
回答 1查看 1K关注 0票数 3

我已经构建了一个npm包,它是Typescript,由于某种原因,在构建时,它不会生成类型定义文件,而当我尝试运行它时,我得到了经典的Could not find a declaration file for module...

我尝试过将typestypings添加到package.json中,但没有成功。

package.json

代码语言:javascript
复制
{
  "name": "@org/mypackage",
  "version": "0.0.7",
  "description": "",
  "main": "index.js",
  "transform": {
    "^.+\\.(ts|tsx)?$": "<rootDir>/node_modules/babel-jest"
  },
  "scripts": {
    "build": "tsc"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/sample/sample.git"
  },
  "author": "Ken M",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/sample/sample/issues"
  },
  "homepage": "https://github.com/sample/sample#readme",
  "dependencies": {
    "@types/jest": "^24.0.13",
    "@types/node": "12.0.2",
    "@types/react": "16.8.17",
    "@types/react-dom": "16.8.4",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1",
    "tachyons": "^4.11.1"
  },
  "devDependencies": {
    "@babel/core": "^7.4.4",
    "@babel/preset-react": "^7.0.0",
    "@typescript-eslint/eslint-plugin": "^1.9.0",
    "@typescript-eslint/parser": "^1.9.0",
    "awesome-typescript-loader": "^5.2.1",
    "babel-jest": "^24.8.0",
    "babel-preset-react": "^6.24.1",
    "eslint": "^5.16.0",
    "eslint-config-prettier": "^4.3.0",
    "eslint-plugin-prettier": "^3.1.0",
    "prettier": "^1.17.1",
    "typescript": "^3.4.5"
  }
}

tsconfig.json

请参阅下面的tsconfig.json。向其中添加declaration: true不会导致生成文件。无论如何,main: index.js也不会被生成。

代码语言:javascript
复制
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "outDir": "./dist",
    "jsx": "react",
    "declaration": true
  },
  "include": ["src/**/*"]
}
EN

回答 1

Stack Overflow用户

发布于 2019-06-13 05:45:16

简短的回答

declaration添加到tsconfig.json文件的compilerOptions部分。

代码语言:javascript
复制
"declaration": true, /* Generates corresponding .d.ts file. */

或者,将您的build脚本更改为包含--declaration

代码语言:javascript
复制
"build": "tsc --declaration"

这两个选项都会导致npm run build生成声明文件。

然后,通过添加指向主声明文件的types属性来更新您的package.json文件。在下面的示例中,我假设您的主声明文件位于./index.d.ts

代码语言:javascript
复制
"main": "index.js",
"types": "index.d.ts",

更多细节

the Publishing docs的开篇部分提供了更多关于答案的详细信息。

还有一些额外的compiler options here。这里有三个在你的情况下可能有用的方法:

生成声明的

  • declarationDir输出目录files.
  • declarationMap为每个相应的file.
  • emitDeclarationOnly生成一个源地图,.d.ts只发出.d.ts声明文件。

当您已经在使用babel将文件从TypeScript转换为JavaScript,并且只想使用TypeScript生成声明文件和执行类型检查时,列表中的最后一个选项很方便。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56568958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档