gpt4 book ai didi

reactjs - Typescript/React 开始获取大量 "variableName is undefined"

转载 作者:搜寻专家 更新时间:2023-10-30 20:59:58 24 4
gpt4 key购买 nike

我不确定发生了什么变化,也许它甚至与 babel 有关,但是当我使用这样的东西时,我开始收到类似 UserControler_1 is undefined 的错误

UserControler.ts

export function signOut() { console.log("Sign Out") }

Page.tsx

import * as React from "react;
import { signOut } from "./UserControler";
import { TouchableWithoutFeedback, Text } from "react-native";

class Page extends React.Component {
_signOut = () => signOut()

render() {
return (
<TouchableWithoutFeedback onPress={this._signOut}>
<Text>Sign Out</Text>
</TouchableWithoutFeedback>
)
}
}

上面的结果是这样的错误

UserControler_1 is undefined

有时它会更具体地出错,即

Can't find variable: signOut

最奇怪的是,如果我将代码改成这样,它就可以正常工作

import * as React from "react;
import { signOut } from "./UserControler";

class Page extends React.Component {
render() {
return (
<TouchableWithoutFeedback onPress={() => signOut}>
<Text>Sign Out</Text>
</TouchableWithoutFeedback>
)
}
}

这里很迷茫

我的配置文件

{
"compilerOptions": {
"moduleResolution": "node",
"module": "es6",
"target": "es6",
"lib": ["es7"],
"allowJs": true,
"checkJs": true,
"jsx": "react-native",
"removeComments": true,
"outDir": "./dist",
"typeRoots": ["node_modules/@types", "./typings"],
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowSyntheticDefaultImports": true,
"strict": true
},
"exclude": ["./node_modules", "./android", "./ios", "./__tests__", "./dist", "./__mocks__"],
"include": ["./src"]
}

这构建到 dist 文件夹中,babel 从那里提取文件,从而使应用程序工作,我的 babelrc

{
"presets": ["react-native"]
}

最佳答案

这似乎是 2.7.1 中引入的 typescript 错误 - 这非常烦人,但好消息是升级到 2.7.2 修复了它。 (至少它解决了我遇到的问题)。

更多信息:https://github.com/Microsoft/TypeScript/issues/21478

关于reactjs - Typescript/React 开始获取大量 "variableName is undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48731550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com