【问题标题】:How can I upgrade the Typescript version used by React Native?如何升级 React Native 使用的 Typescript 版本?
【发布时间】:2020-07-16 00:38:14
【问题描述】:

我在我的 React Native 代码中使用了 import type 声明,它可以在 npx tsc 和 VS Code 内部正常工作。但是当我运行应用程序时,我得到了 Unexpected Token 错误。

如何让 React Native 与 import type 很好地配合使用?在我看来,它必须使用旧版本的 TypeScript,但如果我知道如何配置它,我现在不记得了。

(我尝试过重置缓存、从模拟器中删除应用并重新安装、清理 Xcode 构建。)

编辑:我认为这可能与 @babel/plugin-transform-typescript 的旧版本有关,它是 Metro-react-native-babel-transformer 的一部分,这是 RN 的 package.json 所必需的.

【问题讨论】:

    标签: typescript react-native


    【解决方案1】:

    为了指定项目的依赖项之一使用的包的版本,你可以在你的 package.json 中添加一个resolutions 块:

     "resolutions": {
        "react-native/@babel/plugin-transform-typescript": "<version number>"
    }
    

    【讨论】:

    • 我尝试将 plugin-transform-typescript 升级到最新版本(以及 metro-react-native-babel-preset,其中包括它),但到目前为止没有运气。
    【解决方案2】:

    我能够通过升级到 React Native 0.63.1 来解决这个问题。

    我之前曾尝试仅升级 @babel/plugin-transform-typescript,但没有奏效 - 似乎需要与 RN 63 捆绑的其他东西才能与 TS 3.9 良好配合。

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 2016-09-25
      • 2023-02-01
      • 2020-11-06
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      相关资源
      最近更新 更多