无法在其他项目中导入内置库

问题描述 投票:0回答:1

我需要构建一个作为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.jsonpackage.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文件导入该库吗?
javascript typescript typescript2.0 tsc type-declaration
1个回答
0
投票

要获取打字稿以使用类型定义文件(soapResponseType.d.ts),您需要在类型文件夹中将soapResponseType.d.ts定义为soapResponseType.ts,并且它将生成具有soapResponseType.d.ts确切内容的soapResponseType.ts.ts个文件,不需要编译。另外,您应该考虑将types文件夹移至src文件夹,因为您的tsconfig配置为仅编译src文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.