You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue3+Pinia项目中解决TypeScript 2307模块找不到错误

解决Vue3/Pinia/TypeScript中无法识别Transaction类型的问题

1. 修复类型文件的导出问题

你的Transaction.ts中定义的类型没有对外导出,TypeScript无法将其识别为可导入的模块内容,需要加上export关键字:

// src/types/Transaction.ts
export type Transaction = {
    id: number;
    description: string;
    transactionType: string;
    amount: number;
}

2. 修正导入路径(二选一即可)

方式一:使用相对路径

由于store文件位于src/store目录,类型文件在src/types目录,需要向上回溯一级目录再进入types文件夹,同时导入时无需加.ts后缀:

// src/store/xxx.ts 中替换原导入语句
import type { Transaction } from '../types/Transaction';

(使用import type是更规范的做法,因为仅导入类型,不会在运行时生成额外代码)

方式二:使用别名路径(推荐)

你的tsconfig.json已经配置了@/*指向src/*,直接用别名导入更简洁,且不受文件目录结构变化影响:

import type { Transaction } from '@/types/Transaction';

3. 可选:给state中的数组添加类型标注

为了让TypeScript更明确transactions的类型,可在state中添加类型断言:

state: () => ({ 
    transactions:  [
         { "id": 9, "description": "Salary", "transactionType": "Income", "amount": 3000.00 },
         { "id": 27, "description": "Air Fare", "transactionType": "Expense", "amount": 700.00 },
         { "id": 6, "description": "Taxi", "transactionType": "Expense", "amount": 30.00 }
    ] as Transaction[]
}),

4. 排查缓存问题

如果以上步骤做完仍报错,重启你的开发服务器(比如Vite),或者删除node_modules/.vite目录清除缓存后再重新启动。

内容的提问来源于stack exchange,提问作者Henry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:17:32