将TypeScript添加到现有ReactNative项目时出现问题

rqqzpn5f  于 11个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(142)

所以,我最近决定最好在我的ReactNative项目的某些部分使用TypeScript,我在互联网上搜索,看看是否可能,我发现是的。
我仔细地遵循了ReactNative文档中的“将TypeScript添加到现有项目”,除了 * 第三项,即“将JavaScript文件转换为 *.tsx”,因为我不理解它并感到害怕。
但问题是,我在TypeScript中启动了一个屏幕文件,但我得到了荒谬的错误,对我来说毫无意义。例如:

import { StyleSheet, Text, View } from 'react-native'
import React from 'react'

const HomeAq = () => {
  return (
    <View>
      <Text>HomeAq</Text>
  )
    </View>
}

export default HomeAq

const styles = StyleSheet.create({})

字符串
当我运行npx tsc来检查它时,我得到:

src/Screens/HomeAq.ts:7:21 - error TS1161: Unterminated regular expression literal.

7       <Text>HomeAq</Text>
  

src/Screens/HomeAq.ts:8:7 - error TS1161: Unterminated regular expression literal.

8     </View>
        

Found 2 errors in the same file, starting at: src/Screens/HomeAq.ts:7


我的tsv.json文件似乎是可以接受的:

{
  "extends": "@tsconfig/react-native/tsconfig.json",
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "jsx": "react-native",
    "strict": true,
    "esModuleInterop": true
  },
}


我已经尝试再次运行install命令,重新加载VSCode一百万次,多次检查tsconfig文件,创建数百个新的TypeScript文件,我甚至检查node_modules,看看那里是否发生了异常,但什么都没有。

ar7v8xwq

ar7v8xwq1#

当使用TypeScript时,任何包含JSX代码的文件都需要有.tsx扩展名。.ts将无法工作,这似乎是您从错误消息中使用的。将所有包含JSX代码的文件复制到.tsx应该可以修复您遇到的错误。

相关问题