在libray子项目中找不到命名空间...

时间:2019-05-23 15:13:51

标签: angular

我使用命令WebMidi安装了npm install @types/webmidi类型

node_modules/@types/webmidi/index.d.ts文件包含

declare namespace WebMidi {

我正在尝试使用如下类型:

const midiAccess$: Observable<WebMidi.MIDIAccess> =

VSCode IDE很高兴,并且没有显示任何错误。

但是在外部终端中的命令行上构建项目会显示错误:Cannot find namespace 'WebMidi'

控制台输出显示:

ERROR in projects/midi-lib/src/lib/midi.service.ts(19,37): error TS2503: Cannot find namespace 'WebMidi'.
projects/midi-lib/src/lib/midi.service.ts(20,36): error TS2503: Cannot find namespace 'WebMidi'.
projects/midi-lib/src/lib/midi.service.ts(21,26): error TS2503: Cannot find namespace 'WebMidi'.
projects/midi-lib/src/lib/midi.service.ts(24,29): error TS2503: Cannot find namespace 'WebMidi'.

我怀疑是因为上面的构建命令发生在工作空间中的子项目中。

工作空间结构如下:

midi-lib
├── dist
├── node_modules
└── projects
    ├── midi-app
    ├── midi-app-e2e
    └── midi-lib

node_modules目录内没有midi-lib/projects/midi-lib目录。

我使用以下命令进行构建:

cd midi-lib/projects/midi-lib
ng build midi-lib

我也尝试从工作空间目录进行构建,但是得到了完全相同的错误:

cd midi-lib
ng build midi-lib

package.json目录的midi-lib/projects/midi-lib文件仅包含:

{
  "name": "midi-lib",
  "version": "0.0.1",
  "description": "This is a MIDI Angular Library published to npm.",
  "keywords": [
    "Angular",
    "Library"
  ],
  "license": "SEE LICENSE IN LICENSE",
  "repository": {
    "type": "git",
    "url": "https://github.com/stephaneeyber/midi-library"
  },
  "homepage": "",
  "peerDependencies": {
    "@angular/common": "^7.2.0",
    "@angular/core": "^7.2.0"
  }
}

如何将package.json父目录的midi-lib文件的依赖项包含在子项目目录的package.json文件中?还是如何在子项目目录中使用build命令来考虑package.json父目录的midi-lib文件的依赖性?

我在Angular 7.2.0

更新:我可以通过一种解决方法解决该问题。 在projects/midi-lib/tsconfig.lib.json文件中,我删除了空的types属性,并将其替换为:

"typeRoots": [
  "../../node_modules/@types"
],

然后我可以构建库项目,然后构建工作区:

ng build midi-lib
ng serve

但是我仍然不满意,因为在构建工作区之前,我不想跳过必须构建库项目。

在工作空间目录下的tsconfig.json文件中,我想替换paths属性:

"paths": {
  "midi-lib": [
    "dist/midi-lib"
  ],
  "midi-lib/*": [
    "dist/midi-lib/*"
  ]
}

具有以下内容:

"paths": {
  "midi-lib": [
    "projects/midi-lib/src/public-api"
  ]
}

,但是工作区构建ng serve失败,出现相同的缺少类型定义错误。

工作区tsconfig.json文件:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "midi-lib": [
        "dist/midi-lib"
      ],
      "midi-lib/*": [
        "dist/midi-lib/*"
      ]
    }
  }
}

图书馆项目projects/midi-lib/tsconfig.lib.json文件:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "target": "es2015",
    "module": "es2015",
    "moduleResolution": "node",
    "declaration": true,
    "sourceMap": true,
    "inlineSources": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "typeRoots": [
      "../../node_modules/@types"
    ],
    "lib": [
      "dom",
      "es2018"
    ]
  },
  "angularCompilerOptions": {
    "annotateForClosureCompiler": true,
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "enableResourceInlining": true
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

0 个答案:

没有答案