网站搜索

在我的 Angular 项目中,Typescript 找不到 @types/spotify-api


我安装了 @types/spotify-api 并对我的 tsconfig.json 做了以下更改

"typeRoots": [
  "node_modules/@types"
],
"types": [
  "spotify-api"
],

例如,Typescript 找不到 PlaylistTrackResponse。我也搞不清楚为什么,因为在其他项目中,它对像 @types/node 这样的类型是可以正常工作的。我使用的是 typescript 3.7.5。

我的完整 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "spotify-api"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "disableTypeScriptVersionCheck": true
  }
}

答案#1

不知道你是否还需要这个问题的答案,因为你提问已经有一段时间了,但你可以通过添加以下内容来解决这个问题:

/// <reference types="spotify-api" />

在你的班级的顶端,然后像这样获取任何类型:

SpotifyApi.PlaylistObjectSimplified

答案#2

如果你正在使用 Nx Workspace,并且在创建库之后遇到相同的错误,请确保检查 tsconfig.app.json 并删除 types: []。它会覆盖全局类型设置,从而导致 SpotifyApi 命名空间“消失”