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
相关产品推荐
相关产品推荐

