TSLINT错误:类型“ typeof React”上不存在属性“ Component”

时间:2019-04-25 09:51:31

标签: reactjs typescript react-native

我下载了[react-native-redux-typescript-boilerplate]:https://reactnativeseed.com/。该项目可在android模拟器中顺利运行。但是,尤其是在JSX语法(即.tsx文件)中,存在大量的tslint错误。一个主要的是https://reactnativeseed.com/

我使用webstrom作为我的代码编辑器,然后交叉检查了webstrom和项目使用的打字稿版本。有一样。

这些是版本: 1.打字稿-2.6.2 2.本机-0.59.6 3.反应-16.2.0 4. react-redux-5.0.6 5. @ types / react-“ ^ 16.8.14” 6. @ types / react-native-“ ^ 0.47.7”

我还需要检查吗?

我还安装了[“ tslint-fix”:“ ^ 0.1.3”]:https://www.npmjs.com/package/tslint-fix

ex: index.tsx

import * as React from "react";
import { Item, Input, Icon, Form, Toast } from "native-base";
import { Field, reduxForm } from "redux-form";
import Login from "../../stories/screens/Login";

const required = value => (value ? undefined : "Required");
const maxLength = max => value => (value && value.length > max ? `Must be ${max} characters or less` : undefined);
const maxLength15 = maxLength(15);
const minLength = min => value => (value && value.length < min ? `Must be ${min} characters or more` : undefined);
const minLength8 = minLength(8);
const email = value =>
    value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) ? "Invalid email address" : undefined;
const alphaNumeric = value => (value && /[^a-zA-Z0-9 ]/i.test(value) ? "Only alphanumeric characters" : undefined);

export interface Props {
    navigation: any;
    valid: boolean;
}
export interface State {}
class LoginForm extends React.Component<Props, State> {
    textInput: any;

    renderInput({ input, meta: { touched, error } }) {
        return (
            <Item error={error && touched}>
                <Icon active name={input.name === "email" ? "person" : "unlock"} />
                <Input
                    ref={c => (this.textInput = c)}
                    placeholder={input.name === "email" ? "Email" : "Password"}
                    secureTextEntry={input.name === "password" ? true : false}
                    {...input}
                />
            </Item>
        );
    }

    login() {
        if (this.props.valid) {
            this.props.navigation.navigate("Drawer");
        } else {
            Toast.show({
                text: "Enter Valid Username & password!",
                duration: 2000,
                position: "top",
                textStyle: { textAlign: "center" },
            });
        }
    }

    render() {
        const form = (
            <Form>
                <Field name="email" component={this.renderInput} validate={[email, required]} />
                <Field
                    name="password"
                    component={this.renderInput}
                    validate={[alphaNumeric, minLength8, maxLength15, required]}
                />
            </Form>
        );
        return <Login loginForm={form} onLogin={() => this.login()} />;
    }
}
const LoginContainer = reduxForm({
    form: "login",
})(LoginForm);
export default LoginContainer;

在render()的上述文件中,显示了很多错误。 其中一些是: 1.属性“ Component”在类型“ typeof React”上不存在。 2. JSX元素类型'Item'不是JSX元素的构造函数。  类型“ Item”中缺少属性“ render”。 还有更多类似的东西。

2 个答案:

答案 0 :(得分:1)

我终于解决了这个问题。 @ type / react使用2.8.1版打字稿,而我的项目使用2.6.2版打字稿。因此,我将项目的打字稿版本升级到2.8.1,错误消失了。

答案 1 :(得分:0)

您忘记了导入React

import React, { Component } from 'react';

class LoginForm extends React.Component<Props, State>替换为class LoginForm extends Component<Props, State>