简体   繁体   English

TypeScript 不解析 react-native-web 代码

[英]TypeScript not parsing react-native-web code

Already tried suggestions here and here with no luck已经在这里这里尝试了建议,但没有运气

I have a project with a directory structure like this:我有一个目录结构如下的项目:

.
  - /app
  - /web

app is a react native app (created manually, not with create-react-native-app ). app是一个本机应用程序(手动创建,而不是使用create-react-native-app )。

web is a react app created using create-react-app . web是一个使用create-react-app

Both use TypeScript.两者都使用 TypeScript。

I'm trying to render a component from app in the web application using react-native-web , but TypeScript doesn't seem to be parsing the file as I get this error:我正在尝试使用react-native-webweb应用app中的应用程序呈现一个组件,但是当我收到此错误时,TypeScript 似乎没有解析文件:

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
> export type Props = {

Given that TypeScript is working fine within app and web independently, I'm not sure what I'm missing.鉴于 TypeScript 在appweb独立运行良好,我不确定我错过了什么。 One slight quirk of the project is that, because create-react-app doesn't allow relative imports from outside its own src folder, I've had to symlink app using npm link app in the web directory in order to import the app components.该项目的一个轻微的怪癖是,因为create-react-app不允许从自身以外的相对进口src文件夹,我不得不符号链接app使用npm link appweb目录,以导入应用程序组件.

Not sure if that's causing the problem, but I'm pretty stumped so any suggestions would be greatly appreciated.不确定这是否会导致问题,但我很难过,所以任何建议都将不胜感激。

Solution ended up being to add ts-loader to the webpack config of my React for web app.解决方案最终是将ts-loader添加到我的 React for web 应用程序的 webpack 配置中。 This was made slightly harder by the fact that create-react-app doesn't allow modifying its config files, but I was able to do so using react app rewired由于create-react-app不允许修改其配置文件这一事实使这变得稍微困难​​一些,但我能够使用react app rewired来做到这一点

I also had to change TypeScript's noEmit to false to ensure tsc is used for compilation as well as type checking (had to do this in react-app-rewired too, because CRA overwrites that setting in tsconfig.json if you try to change it).我还必须将 TypeScript 的noEmit更改为false以确保 tsc 用于编译和类型检查(也必须在 react-app- tsconfig.json执行此操作,因为如果您尝试更改它,CRA 会覆盖tsconfig.json该设置) .

Still slightly confusing to me why this was needed, given TypeScript was being parsed perfectly fine in the web and mobile apps individually.考虑到 TypeScript 在 Web 和移动应用程序中单独解析得非常好,我仍然有点困惑为什么需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM