使用 React Native 和 TypeScript 找不到变量符号 [英] Can't find variable Symbol using React Native with TypeScript

查看:62
本文介绍了使用 React Native 和 TypeScript 找不到变量符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 React Native 项目中使用 ES6 Symbol.这适用于 iOS,但在 Android 上我得到 Can't find variable: Symbol.

I'm using the ES6 Symbol in my React Native project. This works fine for iOS, but on Android I get Can't find variable: Symbol.

我正在使用 TypeScript,并且尝试了各种 tsconfig.json:

I'm using TypeScript, and I've tried various tsconfig.json:

{
  "compilerOptions": {
    "target": "es2015",
    "jsx": "react",
    "lib": ["dom", "es7"],
    "moduleResolution": "node"
  },
}

我也试过 lib: ["dom", "es6"]["dom", "es6", "es7"] 以及目标:es5".

I've also tried lib: ["dom", "es6"] and ["dom", "es6", "es7"] as well as target: "es5".

如何让 Symbol 由 TypeScript for React Native 在 Android 上正确处理?

How can I get Symbol to be properly handled by TypeScript for React Native on Android?

推荐答案

您可以使用 import 'core-js/es6/symbol' 而无需添加 npm 依赖项或复制粘贴 polyfill

You can use import 'core-js/es6/symbol' without having to add an npm dependency or copy pasting an polyfill

我怀疑这是一个 TS 问题.Android JS 运行时没有 Symbol 支持.我在普通 JSX 中也遇到了这个问题,并使用上面粘贴的代码段修复了它.

I doubt that it is a TS problem. Android JS runtime doesn't have Symbol support. I had this problem in plain JSX too and fixed it using the snippet pasted above.

这篇关于使用 React Native 和 TypeScript 找不到变量符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆