简体   繁体   English

错误 TS2503:找不到命名空间“WebMidi”

[英]error TS2503: Cannot find namespace 'WebMidi'

I have an Angular workspace containing a library and its testing app.我有一个 Angular 工作区,其中包含一个库及其测试应用程序。

projects/
├── midi-app
│   ├── browserslist
│   ├── karma.conf.js
│   ├── src
│   ├── tsconfig.app.json
│   ├── tsconfig.spec.json
│   └── tslint.json
├── midi-app-e2e
│   ├── protractor.conf.js
│   ├── src
│   └── tsconfig.e2e.json
└── midi-lib
    ├── karma.conf.js
    ├── ng-package.json
    ├── package.json
    ├── README.md
    ├── src
    ├── tsconfig.lib.json
    ├── tsconfig.spec.json
    └── tslint.json

I'm using the @types/webmidi and the VSCode editor is not complaining.我正在使用@types/webmidi并且 VSCode 编辑器没有抱怨。

I can also successfully build the library with the ng build midi-lib --watch and the compilation completes.我还可以使用ng build midi-lib --watch成功构建库并完成编译。

But then, when I try to serve the application usng the ng serve command, from the same directory, it fails with the error TS2503: Cannot find namespace 'WebMidi' error.但是,当我尝试使用ng serve命令从同一目录为应用程序提供服务时,它失败并出现error TS2503: Cannot find namespace 'WebMidi'错误。

It used to serve just fine until yesterday morning when I opened up the laptop and tried to serve the application, when it failed.直到昨天早上,当我打开笔记本电脑并尝试为该应用程序提供服务时,它才正常运行,但它失败了。 I had not done any code change since the day before when it served fine.自从前一天运行良好以来,我没有做过任何代码更改。

I tried cleaning up but it changed nothing:我尝试清理但没有任何改变:

rm -fr node_modules package-lock.json
npm cache clean
npm install

Here are some configuration files.下面是一些配置文件。

The tsconfig.json file: tsconfig.json文件:

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

The tsconfig-lib.json file: 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"
  ]
}

I tried adding我尝试添加

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

in the tsconfig.json file but it didn't help at all.tsconfig.json文件中,但它根本没有帮助。

And I also tried removing我也尝试删除

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

from the tsconfig-lib.json file but it again didn't help.来自tsconfig-lib.json文件,但它再次没有帮助。

UPDATE: The issue is now gone and I have no idea why.更新:问题现在消失了,我不知道为什么。 Is it because I upgraded typescript to 3.2.4 ?是因为我把typescript升级到3.2.4了吗? Or because I removed the webmidi 2.3.3 dependency from the package.json file?或者因为我从package.json文件中删除了webmidi 2.3.3依赖项? High Chaparal...高丛林...

The namespace is not webmidi , this is only the name of the npm package. the namespace is WebMidi , so I'll suggest you add types to both tsconfig.json and tsconfig.spec.ts (for tests ) inside "compilerOptions" .命名空间不是webmidi ,这只是npm package 的名称。命名空间是WebMidi ,因此我建议您在"compilerOptions" tsconfig.jsontsconfig.spec.ts (用于测试)添加类型。

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "allowSyntheticDefaultImports":true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ],
    "types": [ "WebMidi" ],
  }
}

Try this to see if it work 试试这个看看是否可行

In your tsconfig.app.json 在您的tsconfig.app.json中

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": ["webmidi"] // add here
  },
  "exclude": ["test.ts", "**/*.spec.ts"]
}

Instead of tsconfig.json you should add changes to tsconfig.app.json , because this file is used for build.取而代之的tsconfig.json你应该添加更改tsconfig.app.json ,因为这个文件用于构建。 For example my file looks like:例如我的文件看起来像:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

it will work if you remove types field or if you add needed type inside like types: ["webmidi"] .如果您删除types字段或在类似types: ["webmidi"]内添加所需的类型, types: ["webmidi"]

Works for me and I'm sure will works for you对我有用,我相信对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM