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

React Native 0.78中WatermelonDB在JS正常但TS无法运行求助

React Native 0.78 + WatermelonDB TypeScript环境适配问题排查方案

一、修正Babel插件顺序与配置

你当前的Babel插件顺序不符合要求,且装饰器插件缺少关键配置,调整为:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }], // 必须指定legacy模式,且放在类属性插件之前
    ['@babel/plugin-proposal-class-properties', { loose: true }],
  ],
};

WatermelonDB依赖的装饰器语法要求开启legacy: true,且插件加载顺序必须优先于类属性插件

二、修复TypeScript类型依赖安装错误

你安装的类型依赖包名有误,正确包名带s,重新执行:

yarn remove @nozbe/watermelondb-type
yarn add --dev @nozbe/watermelondb-types

三、检查实体类的TypeScript语法规范

确保WatermelonDB实体类的TS写法符合要求,示例:

import { Model } from '@nozbe/watermelondb';
import { field, text } from '@nozbe/watermelondb/decorators';

export default class Post extends Model {
  static table = 'posts';
  
  @text('title') title!: string;
  @field('is_published') isPublished!: boolean;
}

使用!断言属性类型,避免TS严格模式下的初始化报错

四、彻底清理缓存并重启服务

执行以下命令清除所有缓存并重建依赖:

yarn cache clean
rm -rf node_modules ios/Pods ios/Podfile.lock android/.gradle android/build
yarn install
cd ios && pod install && cd ..
yarn start --reset-cache

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:48:14