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

React Native(Expo)项目为何无法自动推断null作为返回类型?

Expo React Native中TypeScript无法推断null返回类型的解决方法

问题核心是expo/tsconfig.base的默认配置大概率关闭了strictNullChecks,导致TypeScript不会将null显式纳入返回类型推断——而你的Web项目应该默认开启了该选项,所以能正常推断出true | null。

解决步骤:

  • 在你的tsconfig.json中显式开启strictNullChecks,覆盖base配置:
{
    "compilerOptions": {
        "strictNullChecks": true
    },
    "extends": "expo/tsconfig.base"
}
  • 或者直接开启完整的strict模式(包含strictNullChecks在内的一系列严格类型检查):
{
    "compilerOptions": {
        "strict": true
    },
    "extends": "expo/tsconfig.base"
}

配置修改后,TypeScript就能和Web项目一样,正确推断包含null的返回类型。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 04:03:13