[英]Custom Typescript NPM Package Won't Generate Types
I've got an npm package that I've built that's Typescript and for some reason when building, it's not generating type definition files and when I try to run it I get the classic Could not find a declaration file for module...
我已经构建了一个npm包,它是Typescript,由于某种原因,它不能生成类型定义文件,当我尝试运行它时,我得到了经典的Could not find a declaration file for module...
I've tried adding types
and typings
to package.json with no luck. 我已经尝试添加types
和typings
,没有运气的package.json。
package.json package.json
{
"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
See tsconfig.json below. 请参阅下面的tsconfig.json。 Adding declaration: true
to it did not result in a file getting generated. 添加declaration: true
不会导致生成文件。 For what it's worth, the main: index.js
isn't getting generated either. 就其价值而言, main: index.js
也未生成。
{
"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/**/*"]
}
Add declaration
to the compilerOptions
section of your tsconfig.json
file. 加入declaration
的compilerOptions
你的部分tsconfig.json
文件。
"declaration": true, /* Generates corresponding .d.ts file. */
Alternatively, change your build
script to include --declaration
. 或者,将build
脚本更改为包括--declaration
。
"build": "tsc --declaration"
Both options will cause npm run build
to generate declaration files. 这两个选项都将导致npm run build
生成声明文件。
Then update your package.json
file by adding a types
property that points to the main declaration file. 然后,通过添加指向主声明文件的types
属性来更新package.json
文件。 In the following example, I have assumed that your main declaration file is at ./index.d.ts
. 在下面的示例中,我假设您的主声明文件位于./index.d.ts
。
"main": "index.js",
"types": "index.d.ts",
The opening section of the Publishing docs have more detail about the answer. Publishing文档的开头部分提供了有关答案的更多详细信息。
There are also additional compiler options here . 这里还有其他编译器选项 。 Here are three that might be useful in your situation: 以下三种情况可能对您有用:
declarationDir
Output directory for generated declaration files. declarationDir
生成的声明文件的输出目录。 declarationMap
Generates a sourcemap for each corresponding .d.ts
file. declarationMap
生成用于每个对应一个sourcemap .d.ts
文件。 emitDeclarationOnly
Only emit .d.ts
declaration files. emitDeclarationOnly
仅发出.d.ts
声明文件。 The last option in the list is handy when you are already using babel
to transform files from TypeScript to JavaScript and only want to use TypeScript to generate declaration files and to perform type checking. 如果您已经在使用babel
将文件从TypeScript转换为JavaScript,并且只想使用TypeScript生成声明文件并执行类型检查,则列表中的最后一个选项非常方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.