无法找到命名空间 ‘SpotifyApi’ (@types/spotify-api) 在 Angular 项目中的错误。

huangapple go评论71阅读模式
英文:

Cannot find namespace 'SpotifyApi' (@types/spotify-api) error in angular project

问题

I have two projects, one api and one client (angular) project. In my angular project I have the following tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "module": "ES2022",
    "useDefineForClassFields": false,
    "lib": ["ES2022", "dom"],
    "paths": {
      "@shared/*": ["../api/src/*"]
    }
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

As you can see I added the paths object in order to import files from "../api/src/*" (the api project). This is working fine, only I have a file at "../api/src/schema/request.d.ts":

export namespace Request {
  export interface GetPlaylistTracks {
    Params: {
      playlistId: string;
    };
    ResBody: SpotifyApi.PagingObject<any> | undefined;
    ReqBody: {};
    ReqQuery: {
      offset: string;
      limit: string;
    };
  }
}

which I'm trying to import inside the angular project like so

import { Request } from '@shared/schema/request';

getting the error

Error: ../api/src/schema/request.d.ts:26:14 - error TS2833: Cannot find namespace 'SpotifyApi'. Did you mean 'Spotify'?

26     ResBody: SpotifyApi.PagingObject<any> | undefined;

The namespace SpotifyApi is coming from @types/spotify-api (which is working fine inside my api project). In both projects inside my package.json I have

{
  ...
  "devDependencies": {
    ...
    "@types/spotify-api": "^0.0.22"
  }
}
英文:

I have two projects, one api and one client (angular) project. In my angular project I have the following tsconfig.json:

{
  &quot;compileOnSave&quot;: false,
  &quot;compilerOptions&quot;: {
    &quot;baseUrl&quot;: &quot;./&quot;,
    &quot;outDir&quot;: &quot;./dist/out-tsc&quot;,
    &quot;forceConsistentCasingInFileNames&quot;: true,
    &quot;strict&quot;: true,
    &quot;noImplicitOverride&quot;: true,
    &quot;noPropertyAccessFromIndexSignature&quot;: true,
    &quot;noImplicitReturns&quot;: true,
    &quot;noFallthroughCasesInSwitch&quot;: true,
    &quot;sourceMap&quot;: true,
    &quot;declaration&quot;: false,
    &quot;downlevelIteration&quot;: true,
    &quot;experimentalDecorators&quot;: true,
    &quot;moduleResolution&quot;: &quot;node&quot;,
    &quot;importHelpers&quot;: true,
    &quot;target&quot;: &quot;ES2022&quot;,
    &quot;module&quot;: &quot;ES2022&quot;,
    &quot;useDefineForClassFields&quot;: false,
    &quot;lib&quot;: [&quot;ES2022&quot;, &quot;dom&quot;],
    &quot;paths&quot;: {                          /* &lt;-- note this bit */
      &quot;@shared/*&quot;: [&quot;../api/src/*&quot;]
    }
  },
  &quot;angularCompilerOptions&quot;: {
    &quot;enableI18nLegacyMessageIdFormat&quot;: false,
    &quot;strictInjectionParameters&quot;: true,
    &quot;strictInputAccessModifiers&quot;: true,
    &quot;strictTemplates&quot;: true
  }
}

As you can see I added the paths object in order to import files from &quot;../api/src/*&quot; (the api project). This is working fine, only I have a file at &quot;../api/src/schema/request.d.ts&quot;:

export namespace Request {
  export interface GetPlaylistTracks {
    Params: {
      playlistId: string;
    };
    ResBody: SpotifyApi.PagingObject&lt;any&gt; | undefined;
    ReqBody: {};
    ReqQuery: {
      offset: string;
      limit: string;
    };
  }
}

which I'm trying to import inside the angular project like so

import { Request } from &#39;@shared/schema/request&#39;;

getting the error

Error: ../api/src/schema/request.d.ts:26:14 - error TS2833: Cannot find namespace &#39;SpotifyApi&#39;. Did you mean &#39;Spotify&#39;?

26     ResBody: SpotifyApi.PagingObject&lt;any&gt; | undefined;

The namespace SpotifyApi is coming from @types/spotify-api (which is working fine inside my api project). In both projects inside my package.json I have

{
  ...
  &quot;devDependencies&quot;: {
    ...
    &quot;@types/spotify-api&quot;: &quot;^0.0.22&quot;
  }
}

答案1

得分: 0

你可以尝试将“@types/spotify-api”: “^0.0.22” 添加到依赖项中,而不是将其放在devDependencies中。

  • "dependencies": 在生产环境中需要的应用程序所需的包。
  • "devDependencies": 仅在本地开发和测试时需要的包。

由于您在模型中引用了SpotifyApi.PagingObject,因此您需要将其添加到依赖项中。

英文:

Instead of having on devDependencies Did you try adding "@types/spotify-api": "^0.0.22" to dependencies.

  • "dependencies": Packages required by your application in production.
  • "devDependencies": Packages that are only needed for local
    development and testing.

Since you are referring the SpotifyApi.PagingObjec in a model. You need yo add it to dependencies.

答案2

得分: 0

我可以通过将以下内容添加到 request.d.ts 文件来解决这个问题:

import 'spotify-api';

不过我不知道为什么这是必要的。顺便说一下,当不使用 paths 属性时,即在任何 Angular 项目中都不起作用,只在我的后端项目中起作用。VS Code 没有显示任何错误,错误只出现在终端/ Web 客户端中。

英文:

I could solve the problem by adding

import &#39;spotify-api&#39;;

to request.d.ts file. Still I don't know why this is necessary. Btw., I'm having the same problem when not using paths property, i.e. SpotifyApi is not working inside any angular project, only in my backend project. VS Code is not showing any errors, they just appear in the terminal/web client.

huangapple
  • 本文由 发表于 2023年6月26日 15:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定