我有一个返回 React Native 的 TypeScript 函数View
。
import React from "react";
import JSX, {View} from "react-native";
class DummyClass {
static getView() {
return (
<View style={{flex: 1}}/>
);
}
}
export default DummyClass;
我以这种方式调用这个函数:
import JSX from "react-native";
import DummyClass from "./util/dummy";
const DummyWrapper = () => {
return (DummyClass.getView());
};
export default DummyWrapper;
当我跑步时eslint
,我会收到警告。
5:5 警告缺少函数 @typescript-eslint/explicit-module-boundary-types 的返回类型
所以,我需要返回一些东西。JSX.Element
似乎足够合理,但这似乎不起作用。一方面,当我尝试返回 a 时JSX.Element
,VSCode 无法解决它并假装它是any
。而且,它会导致调用该函数的其他地方出错。
所以,这样做:
static getView() : JSX.Element {
和const DummyWrapper = () : JSX.Element => {
导致以下错误DummyCaller
:
5:5 错误任何类型值的不安全返回 @typescript-eslint/no-unsafe-return
所以现在我不确定该怎么做。我尝试了其他一些事情,例如返回View
、、typeof View
(React.Component
和typeof
)以及其他一些事情。我还搞砸了 JSX 是从哪里导入的。如果我从 导入react
,它似乎比从 导入它的效果更差react-native
。同样在我的研究中,我发现最常见的问题是 React 和 React Native 类型已经过时,但据我所知,我使用的是最新版本。
我的packages.json
:
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject",
"lint": "eslint -c .eslintrc.js --ext .tsx ."
},
"dependencies": {
"@dudigital/react-native-zoomable-view": "^1.0.15",
"@react-native-community/masked-view": "^0.1.10",
"@react-navigation/native": "^5.7.6",
"@react-navigation/stack": "^5.9.3",
"@types/react-native-vector-icons": "^6.4.6",
"change-case": "^4.1.1",
"expo": "~39.0.2",
"expo-status-bar": "~1.0.2",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.2.tar.gz",
"react-native-gesture-handler": "^1.8.0",
"react-native-paper": "^4.2.0",
"react-native-reanimated": "^1.13.1",
"react-native-safe-area-context": "^3.1.8",
"react-native-screens": "^2.11.0",
"react-native-tab-view": "^2.15.2",
"react-native-vector-icons": "^7.1.0",
"react-native-web": "^0.13.18"
},
"devDependencies": {
"@babel/core": "~7.9.0",
"@types/jest": "^26.0.15",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"@types/react-native": "^0.63.27",
"@types/react-navigation": "^3.4.0",
"@types/react-redux": "^7.1.9",
"@types/react-test-renderer": "^16.9.3",
"@typescript-eslint/eslint-plugin": "^4.5.0",
"@typescript-eslint/parser": "^4.5.0",
"commonjs": "latest",
"eslint": "^7.11.0",
"eslint-plugin-jsdoc": "^30.7.3",
"eslint-plugin-prefer-arrow": "^1.2.2",
"eslint-plugin-react": "^7.21.5",
"react-native-typescript-transformer": "^1.2.13",
"requirejs": "latest",
"ts-jest": "^26.4.1",
"tslib": "^2.0.3",
"typescript": "^4.0.3"
},
"private": true
}