我需要构建一个作为soap API包装器的库。该库的结构如下:
|-node_modules
|
|-src
| |-soapWrapperLibrary.ts
| |-soapLibraryClient.ts
|
|-types
| |-soapResponseType.d.ts
library
使用client
,它返回我定义的对象soapResponseType
。
此库将在另一个实习项目中使用。无需将任何内容推入npm或类似内容。我尝试使用tsc
进行编译,但是编译结果忽略了我定义的类型(soapResponseType.d.ts)。导入我的库时导致错误,因为找不到定义文件。
我已经尝试了几种与typeRoots
的已编译类型相关的方法,但没有结果。
我希望从tsc
中将我的所有文件编译为一个捆绑包或可以整体导入的捆绑包,其中包含类型和类。
使用以下选项,我唯一得到的是下一个结构,没有任何soapResponseType.d.ts的痕迹:
|-dist
| |-lib
| | |-soapWrapperLibrary.js
| | |-soapWrapperLibrary.js.map
| | |soapLibraryClient.js
| | |soapLibraryClient.js.map
| |-types
| | |-soapWrapperLibrary.d.ts
| | |soapLibraryClient.d.ts
命令tsc --listFiles
显示了我创建的三个文件:两个.ts
文件和一个.d.ts
用于复制的回购https://github.com/TheoSl93/buildLibraryTest
这些是我的tsconfig.json
和package.json
:
tsconfig
{
"compilerOptions": {
"target": "es5",
"module":"es2015",
"strict": true,
"declaration": true,
"outDir": "dist/lib",
"declarationDir": "dist/types",
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"removeComments": true,
"sourceMap": true,
"baseUrl": ".",
"paths": {
"@/*": [
"./types"
]
},
"lib": ["es2015", "es2016", "es2017", "dom"],
"emitDecoratorMetadata": true,
"typeRoots": [
"node_modules/@types",
"types/*"
]
},
"typedocOptions": {
"mode": "modules",
"out": "docs",
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"test/**/*.ts",
"test/**/*.tsx"
],
"exclude": [
"node_modules",
"test"
],
}
package
"name": "soap-library",
"version": "0.0.1",
"description": "reproduce for stackoverflow",
"main": "dist/lib/soapLibraryClient.js",
"repository": "https://github.com/TheoSl93/buildLibraryTest.git",
"author": "Theo Sloot",
"license": "MIT",
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc --module commonjs"
},
"sideEffects": false,
至此:
tsc
,有没有办法在编译器中包含自制的定义?.ts
文件而不是已编译的.js
文件导入该库吗?要获取打字稿以使用类型定义文件(soapResponseType.d.ts
),您需要在类型文件夹中将soapResponseType.d.ts
定义为soapResponseType.ts
,并且它将生成具有soapResponseType.d.ts
确切内容的soapResponseType.ts
是.ts
个文件,不需要编译。另外,您应该考虑将types
文件夹移至src
文件夹,因为您的tsconfig配置为仅编译src
文件夹。