首页 > 解决方案 > 使用 React Native 和 typescript 创建应用程序

问题描述

我正在尝试使用 React Native 和 TypeScript 创建一个应用程序,但它不起作用。我使用以下命令:

npx react-native init MyApp --template react-native-template-typescript

和日志错误:

Yarn version: 
  1.22.15

Node version: 
  16.10.0

Platform: 
  win32 x64

Trace: 
  Error: https://registry.yarnpkg.com/react-native-template-react-native-template-typescript: Not found
      at Request.params.callback [as _callback] (C:\Users\pc\AppData\Roaming\npm\node_modules\yarn\lib\cli.js:66140:18)
      at Request.self.callback (C:\Users\pc\AppData\Roaming\npm\node_modules\yarn\lib\cli.js:140885:22)
      at Request.emit (node:events:390:28)
      at Request.<anonymous> (C:\Users\pc\AppData\Roaming\npm\node_modules\yarn\lib\cli.js:141857:10)
      at Request.emit (node:events:390:28)
      at IncomingMessage.<anonymous> (C:\Users\pc\AppData\Roaming\npm\node_modules\yarn\lib\cli.js:141779:12)
      at Object.onceWrapper (node:events:509:28)
      at IncomingMessage.emit (node:events:402:35)
      at endReadableNT (node:internal/streams/readable:1343:12)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)

标签: typescriptreact-native

解决方案


您应该首先卸载以前的 react-native-cli 以安装具有 typescript 功能的最新版本。

1- npm 卸载 -g react-native-cli。

2-纱线全局添加@react-native-community/cli

3- react-native init MyApp --template react-native-template-typescript


推荐阅读