使用 Typescript 和 Next.js 找不到具有相应类型声明的模块

时间:2021-07-07 22:06:00

标签: node.js reactjs typescript next.js babeljs

我有一个 next.js 项目,我正在尝试向其中添加打字稿。

我的文件夹结构如下:

api
  aggregation.ts
interfaces
  index.ts
components
  Component1
    index.js
    index.module.css
  Component2
    index.js
    index.module.css
pages
  page1
    index.js
    index.module.css

我在 interfaces 中有 next.config.js 的别名

config.resolve.alias.interfaces = path.join(__dirname, 'interfaces');

现在是实际问题

index.ts 中的

interfaces 导出接口如

 export interface Order {
  store: Store;
  orderNumber: string;
  totals: Totals;
  customer: Customer;
  currency: Currency;
  paymentMethod: PaymentMethod;
  items: OrderProduct[];
  createdAt: string;
  orderId: string;
}

我正在尝试将它们导入到 aggregation.ts 中,例如

import { Order } from 'interfaces';

我正在接受

Cannot find module 'interfaces' or its corresponding type declarations.

这也会导致构建失败。

我已经安装

"@types/node": "^16.0.1",
"@types/react": "^17.0.14",
"typescript": "^4.3.5",

接下来用

正确生成了next-env.d.ts
/// <reference types="next" />
/// <reference types="next/types/global" />

有关如何解决 Cannot find module 问题的任何想法?我有一种感觉,它可能与我的文件夹结构或我的构建步骤有关。但我也得到了语法高亮指示导入错误,所以它可能与 typescript 配置相关。

任何建议将不胜感激!

0 个答案:

没有答案