tsconfig.json不能识别路径”@types”

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

tsconfig.json doesn't recognize path "@types"

问题

以下是翻译好的内容:

  1. 我为我的 Next.js 应用程序设置了多个绝对路径。 我在另一个文件夹中时有问题,尝试从绝对路径导入组件时,它会像 **"../componentName"** 这样导入,而不是 **"@components/componentName"**。 我正在使用 vsCode,有时必须手动调整导入以保持一致性。
  2. 我的最后一个问题是,我有一个包含所有 TypeScript 类型的文件夹,名为 **/types**,但当我创建对它的路径时,我不断收到一个错误,说它未声明。
  3. tsconfig.json:
  4. {
  5. "compilerOptions": {
  6. "target": "es5",
  7. "lib": ["dom", "dom.iterable", "esnext"],
  8. "allowJs": true,
  9. "skipLibCheck": true,
  10. "strict": true,
  11. "forceConsistentCasingInFileNames": true,
  12. "noEmit": true,
  13. "esModuleInterop": true,
  14. "module": "esnext",
  15. "moduleResolution": "node",
  16. "resolveJsonModule": true,
  17. "isolatedModules": true,
  18. "jsx": "preserve",
  19. "incremental": true,
  20. "plugins": [
  21. {
  22. "name": "next"
  23. }
  24. ],
  25. "baseUrl": "./",
  26. "paths": {
  27. "@images/*": ["images/*"],
  28. "@lib/*": ["lib/*"],
  29. "@components/*": ["components/*"],
  30. "@types/*": ["types/*"],
  31. "@styles/*": ["styles/*"]
  32. }
  33. },
  34. "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  35. "exclude": ["node_modules"]
  36. }
  37. /types/index.ts
  38. export type { SocialFields } from "./objects/social-fields";
  39. export type { HeroProps } from "./hero";
  40. export type { ImageProps } from "./image";
  41. export type { PostProps } from "./post";
  42. export type { ReadMoreProps } from "./read-more";
  43. export type { SocialProps } from "./socials";
  44. export type { Tag } from "./tag";
  45. BlogCard.tsx:
  46. import { PostProps } from "@types"; // <== 无法找到模块 '@types' 或其相应的类型声明.ts(2307)
英文:

I have setup multiple absolute paths for my Next.js application. I have problems where when I am inside another folder and trying to import a component from the absolute path it will import like "../componentName" instead of "@components/componentName". I am using vsCode and sometimes I have to manually adjust the import for consistency.

My last problem is that I have a folder with all my TypeScript types folder is called /types but when I create a path to it I keep getting an error that it's not declared.

tsconfig.json:

  1. {
  2. "compilerOptions": {
  3. "target": "es5",
  4. "lib": ["dom", "dom.iterable", "esnext"],
  5. "allowJs": true,
  6. "skipLibCheck": true,
  7. "strict": true,
  8. "forceConsistentCasingInFileNames": true,
  9. "noEmit": true,
  10. "esModuleInterop": true,
  11. "module": "esnext",
  12. "moduleResolution": "node",
  13. "resolveJsonModule": true,
  14. "isolatedModules": true,
  15. "jsx": "preserve",
  16. "incremental": true,
  17. "plugins": [
  18. {
  19. "name": "next"
  20. }
  21. ],
  22. "baseUrl": "./",
  23. "paths": {
  24. "@images/*": ["images/*"],
  25. "@lib/*": ["lib/*"],
  26. "@components/*": ["components/*"],
  27. "@types/*": ["types/*"],
  28. "@styles/*": ["styles/*"]
  29. }
  30. },
  31. "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  32. "exclude": ["node_modules"]
  33. }

/types/index.ts

  1. export type { SocialFields } from "./objects/social-fields";
  2. export type { HeroProps } from "./hero";
  3. export type { ImageProps } from "./image";
  4. export type { PostProps } from "./post";
  5. export type { ReadMoreProps } from "./read-more";
  6. export type { SocialProps } from "./socials";
  7. export type { Tag } from "./tag";

BlogCard.tsx:

  1. import { PostProps } from "@types"; // <== Cannot find module '@types' or its corresponding type declarations.ts(2307

答案1

得分: 1

我遇到了类似的问题,我认为这是因为 @types/ 通常与默认的 @type 依赖项引入发生冲突(例如 @types/react, @types/react-dom, @types/node 等),这些依赖项安装在通常被 Git 忽略的 node_modules 目录内(例如 node_modules/@types/)。

因此,我认为(不是100%确定)即使你在 tsconfig.json 中手动配置了路径映射,使用给定的配置;"@types/*": ["types/*"],TypeScript 也会忽略它,因为它有自己内置的映射,将 @types/ 映射到默认情况下只查找 node_modules 目录内的内容。

可能的解决方案 (?)

你可以尝试将以下行添加到 tsconfig.json 文件中:

  1. {
  2. "compilerOptions": {
  3. // ...
  4. "typeRoots": [
  5. "./node_modules/@types",
  6. "./types"
  7. ]
  8. }
  9. }

但在我的情况下,它仍然不起作用。 值得知道为什么不起作用。

替代建议

所以最终,我选择使用 @/ 来映射所有自定义路径(注意斜杠后缀)以保持一致性。这样,我可以区分官方安装的依赖项(例如 @type 来自 node_modules)和自定义项目源代码目录。

例如:在 tsconfig.json 内配置以下路径:

  1. {
  2. "compilerOptions&quot: {
  3. // ...
  4. "paths": [
  5. "@/components/*": ["./src/components/*"],
  6. "@/types/*": ["./src/types/*"]
  7. ]
  8. }
  9. }

然后,在源代码脚本文件内(例如 .tsx 文件),我们可以使用 @/ 前缀缩写导入类型和组件:

  1. import { SomeType } from "@/types/someType";
  2. import SomeComponent from "@/components/some-component/SomeComponent";
英文:

I have stumbled a similar issue, and i believe it is because @types/ typically conflicts with the default @type dependency imports (e.g. @types/react, @types/react-dom, @types/node, etc), which are installed inside the typically git ignored node_modules directory (e.g. node_modules/@types/).

And so I believe (not 100% sure) that even when you have manually configured a path mapping from within tsconfig.json, with the given configuration; "@types/*": ["types/*"], TypeScript will ignore this, as it has its own built-in mapping for @types/ to only (by default) look inside the node_modules directory.

Possible Solution (?)

You could possibly try adding this line to the tsconfig.json file:

  1. {
  2. "compilerOptions": {
  3. // ...
  4. "typeRoots": [
  5. "./node_modules/@types",
  6. "./types"
  7. ]
  8. }
  9. }

But in my case it still did not work. Would be good to know why.

Alternative Suggestion

So I ended up mapping with @/ for all custom paths (note the forward slash suffix) for consistency. That way, I can differentiate between the officially installed dependencies (e.g. @type from node_modules), and the custom project source code directories.

For example: Configure the following paths inside tsconfig.json:

  1. {
  2. "compilerOptions": {
  3. // ...
  4. "paths": [
  5. "@/components/*": ["./src/components/*"],
  6. "@/types/*": ["./src/types/*"]
  7. ]
  8. }
  9. }

..and then from inside a source code script file (e.g. .tsx), we could import types and components using the @/ prefix shorthand:

  1. import { SomeType } from "@/types/someType";
  2. import SomeComponent from "@/components/some-component/SomeComponent";

huangapple
  • 本文由 发表于 2023年2月14日 18:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446821.html
匿名

发表评论

匿名网友

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

确定