在React-Native项目上使用带有TypeScript的React Fragment

ale*_*ngn 5 typescript reactjs react-native

我刚刚在React-Native项目上将React升级到16.2.0,以使用新的Fragment语法。

我有一个组件现在返回类似以下内容:

return (
  <>
    <View>...</View>
    <View>...</View>
  </>
);
Run Code Online (Sandbox Code Playgroud)

TypeScript不会抱怨语法很棒,但是当我尝试运行此代码时出现错误:

Bundling `index.js`  [development, non-minified]  45.8% (1078/1593), failed.
error: bundling failed: SyntaxError in /Users/alexmngn/Workspace/MyProject/MyComponent.tsx: /Users/alexmngn/Workspace/MyProject/MyComponent.tsx: Unexpected token (68:17)
> 68 |         return (<>
     |                  ^
Run Code Online (Sandbox Code Playgroud)

似乎没有将片段语法编译回ES5。

当我尝试使用它时,React.Fragment出现TypeScript错误:

/Users/alexmngn/Workspace/MyProject/MyComponent.tsx(1,28): error TS2305: 
Module '"/Users/alexmngn/Workspace/MyProject/node_modules/@types/react/index"' 
has no exported member 'Fragment'.
Run Code Online (Sandbox Code Playgroud)

该应用程序也不会运行并引发错误 Invariant Violation

我使用所有软件包的最新稳定版本:

  • 反应:16.2.0
  • React-Native:0.51.0
  • 打字稿:2.6.2
  • @类型/反应:16.0.31
  • @ types / react-native:0.51.4

我需要做些什么使其与React-Native一起使用吗?要编译到ES5?或者甚至能够在没有Typescript抱怨的情况下使用React.Fragment?似乎在类型ReactFragment的定义中有一个类型,但仍然出现此错误。

谢谢