简体   繁体   English

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

[英]TSLINT Error: Property 'Component' does not exist on type 'typeof React'

I downloaded the [react-native-redux-typescript-boilerplate]: https://reactnativeseed.com/ .我下载了 [react-native-redux-typescript-boilerplate]: https ://reactnativeseed.com/。 The project runs smoothly in an android emulator.该项目在android模拟器中运行流畅。 However, there are loads of tslint errors especially in the JSX syntax (ie, .tsx files).但是,存在大量 tslint 错误,尤其是在 JSX 语法(即 .tsx 文件)中。 One major one is https://reactnativeseed.com/一个主要的是https://reactnativeseed.com/

I use webstrom as my code editor, and I crossed checked the version of typescript being used by webstrom and the project.我使用 webstrom 作为我的代码编辑器,我交叉检查了 webstrom 和项目使用的打字稿版本。 There are the same.有一样的。

These are the versions: 1. Typescript - 2.6.2 2. react-native - 0.59.6 3. react - 16.2.0 4. react-redux - 5.0.6 5. @types/react - "^16.8.14" 6. @types/react-native - "^0.47.7"这些是版本: 1. Typescript - 2.6.2 2. react-native - 0.59.6 3. react - 16.2.0 4. react-redux - 5.0.6 5. @types/react - "^16.8.14" 6. @types/react-native - “^0.47.7”

Is there anything else I need to check for?还有什么我需要检查的吗?

I have also installed ["tslint-fix": "^0.1.3"]: https://www.npmjs.com/package/tslint-fix我还安装了 ["tslint-fix": "^0.1.3"]: https://www.npmjs.com/package/tslint-fix

ex: index.tsx例如: 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;

in the above file in the render() a lot of errors are shown.在 render() 的上述文件中显示了很多错误。 some of it are: 1. Property 'Component' does not exist on type 'typeof React'.其中一些是: 1. 类型“typeof React”上不存在属性“Component”。 2. JSX element type 'Item' is not a constructor function for JSX elements. 2. JSX 元素类型“Item”不是 JSX 元素的构造函数。 Property 'render' is missing in type 'Item'.类型“Item”中缺少属性“render”。 and many more similar one.还有更多类似的。

I finally resolved the issue.我终于解决了这个问题。 @types/react was using typescript version 2.8.1 while my project was using typescript version 2.6.2. @types/react 使用的是 typescript 版本 2.8.1,而我的项目使用的是 typescript 版本 2.6.2。 So, I upgraded my project's typescript version to 2.8.1 and the errors were gone.因此,我将项目的打字稿版本升级到 2.8.1,错误消失了。

You forgot to import React你忘了导入 React

import React, { Component } from 'react';

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

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

相关问题 React中的'Window&typeof globalThis'类型上不存在'属性'ethereum''错误 - `Property 'ethereum' does not exist on type 'Window & typeof globalThis'` error in React 类型&#39;typeof __React&#39;上不存在属性&#39;render&#39; - Property 'render' does not exist on type 'typeof __React' 属性&#39;findDOMNode&#39;在&#39;typeof React&#39;类型上不存在 - Property 'findDOMNode' does not exist on type 'typeof React' 属性“ typeof CommonStore”类型不存在。 mobx反应的 - Property does not exist on type 'typeof CommonStore'. mobx-react 在TSX文件中:属性&#39;createRef&#39;在类型&#39;typeof React&#39;上不存在 - In TSX file : Property 'createRef' does not exist on type 'typeof React' 属性 '_env_' 不存在于类型 'Window &amp; typeof globalThis - React / Typescript 错误 - property '_env_' does not exist on type 'Window & typeof globalThis - React / Typescript ERROR 类型 Window 和 typeof globalThis 上不存在属性 - Property does not exist on type Window & typeof globalThis 类型“typeof client”上不存在属性“calendar” - Property 'calendar' does not exist on type 'typeof client' 类型“typeof TextInput”上不存在属性“propTypes” - Property 'propTypes' does not exist on type 'typeof TextInput' 类型&#39;typeof FactoryMethod上不存在属性&#39;propTypes&#39; - Property 'propTypes' does not exist on type 'typeof FactoryMethod
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM