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

Tsconfig自定义路径添加/*仍无法生效,模块查找失败求助

解决tsconfig自定义路径不生效的问题

问题描述

自定义路径配置后始终报错:
Cannot find module '@data/interfaces' or its corresponding type declarations.
已尝试在路径后添加/*,但问题仍存在。当前tsconfig配置如下:

{
  "extends": "expo/tsconfig.base",
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitAny": true,
    "allowUnreachableCode": false,
    "strictFunctionTypes": true,
    "paths": {
      "@assets/*": ["assets/*"],
      "@lottie/*": ["assets/lottie/*"],
      "@components/*": ["components/*"],
      "@context/*": ["context/*"],
      "@data*": ["data/*"],
      "@features/*": ["features/*"],
      "@lib/*": ["lib/*"],
      "@locales/*": ["locales/*"],
      "@navigators/*": ["navigators/*"],
      "@services/*": ["services/*"],
      "@utils/*": ["utils/*"],
      "@accounting/*": ["features/accounting/*"],
      "@auth/*": ["features/auth/*"],
      "@home/*": ["features/home/*"],
      "@production/*": ["features/production/*"],
      "@repair/*": ["features/repair/*"],
      "@sale/*": ["features/sale/*"],
      "@scanner/*": ["features/scanner/*"],
      "@settings/*": ["features/settings/*"],
      "@warehouse/*": ["features/warehouse/*"],
    },
    "baseUrl": "src"
  }
}

修复步骤

1. 修正路径语法错误

配置里的@data*缺少斜杠,TypeScript无法正确匹配@data/interfaces这类路径,需修改为:

"@data/*": ["data/*"],

2. 配置Babel支持路径解析

Expo项目仅靠tsconfig配置不足以让打包器识别自定义路径,需安装并配置babel-plugin-module-resolver:

  • 安装依赖:
npm install --save-dev babel-plugin-module-resolver
  • 修改项目根目录的babel.config.js(无则新建),添加插件配置:
module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      [
        'module-resolver',
        {
          root: ['./src'],
          extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
          alias: {
            '@assets': './src/assets',
            '@lottie': './src/assets/lottie',
            '@components': './src/components',
            '@context': './src/context',
            '@data': './src/data',
            '@features': './src/features',
            '@lib': './src/lib',
            '@locales': './src/locales',
            '@navigators': './src/navigators',
            '@services': './src/services',
            '@utils': './src/utils',
            '@accounting': './src/features/accounting',
            '@auth': './src/features/auth',
            '@home': './src/features/home',
            '@production': './src/features/production',
            '@repair': './src/features/repair',
            '@sale': './src/features/sale',
            '@scanner': './src/features/scanner',
            '@settings': './src/features/settings',
            '@warehouse': './src/features/warehouse',
          }
        }
      ]
    ]
  };
};

3. 重启开发服务器

修改配置后必须重启Expo开发服务器,新配置才能生效。

4. 检查目录结构

确认src/data/interfaces.ts(或对应文件/目录)真实存在,路径匹配无误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 11:17:04