函数上缺少返回类型-在React(TypeScript)代码中

谷物

在我的App.tsx中,我得到了:

  • function.eslint(@ typescript-eslint / explicit-function-return-type)缺少返回类型

在我的主要课堂组件中,我得到了这些:

  • 方法定义render.eslint(@ typescript-eslint / explicit-member-accessibility)缺少可访问性修饰符
  • function.eslint(@ typescript-eslint / explicit-function-return-type)缺少返回类型

我正在使用带有TypeScript的React。对于linter,我使用ESLint并用于将代码格式化为Prettier。

我找到了这个信息:https : //github.com/typescript-eslint/typescript-eslint/blob/v1.6.0/packages/eslint-plugin/docs/rules/explicit-function-return-type.md,但是我没有不知道如何以及在哪里使用它。

应用程序

class Main extends Component {
    render() {
        return (
            <Router>
                <div>
                    <Link to="/">Home</Link>
                    <br />
                    <Link to="/component1">Component1</Link>
                    <br />
                    <Link to="/component2">Component2</Link>

                    <Route exact path="/" render={() => <h1>Home Page</h1>} />
                    <Route path="/component1" component={Component1} />
                    <Route path="/component2" component={Component2} />
                </div>
            </Router>
        );
    }
}

Component1.tsc

interface Props {
    number: number;
    onNumberUp: any;
    onNumberDown: any;
}

const Component1 = (props: Props): JSX.Element => {
    return (
        <div>
            <h1>Component1 content</h1>
            <p>Number: {props.number}</p>
            <button onClick={props.onNumberDown}>-</button>
            <button onClick={props.onNumberUp}>+</button>
        </div>
    );
};

const mapStateToProps = (state: any) => {
    return {
        number: state.firstReducer.number,
    };
};

const mapDispachToProps = (dispach: any) => {
    return {
        onNumberUp: () => dispach({ type: 'NUMBER_UP' }),
        onNumberDown: () => dispach({ type: 'NUMBER_DOWN' }),
    };
};

减速器和操作位于单独的文件夹中。

Component1和Component2相似。

有人知道如何解决此错误吗?

mbdavis
Missing accessibility modifier on method definition render.eslint(@typescript-eslint/explicit-member-accessibility)

可访问性修饰符是公共/私有/受保护的。对于渲染,这应该是公共的。

因此,将public一词添加到render():

class Main extends Component {
    public render() {
        ...
    }
}


Missing return type on function.eslint(@typescript-eslint/explicit-function-return-type)

您不应该在这里遇到该错误。这是告诉您添加一个返回类型以进行渲染,但是由于您已经扩展了React.Component,它应该能够从类型定义中加载该类型。

您是否已在项目中添加@ types / react和@ types / react-dom?

如果不, npm i -D @types/react @types/react-dom

看起来您也需要@types/reduxredux代码:(npm i -D @types/redux)从“ redux”导入{Dispatch};

const mapDispatchToProps = (dispatch: Dispatch<any>) => {
    return {
        onNumberUp: () => dispatch({ type: 'NUMBER_UP' }),
        onNumberDown: () => dispatch({ type: 'NUMBER_DOWN' }),
    };
};

最后说明-我不喜欢ESLint中的公共/私有访问者规则。我会禁用它。此处的更多信息(第1点):https : //medium.com/@martin_hotell/10-typescript-pro-tips-patterns-with-or-without-react-react-5799488d6680

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

样式元件插值函数中的“函数上缺少返回类型”错误

Jest / eslint中的函数缺少返回类型

错误在nodejs中缺少函数的返回类型

TYPESCRIPT:函数缺少结束返回语句并且返回类型不包括“未定义”

ESLint - 函数反应组件上缺少返回类型 - Typescript 错误解释

反应:缺少函数的返回类型。eslint(@ typescript-eslint / explicit-function-return-type)

打字稿函数中的错误 - 函数缺少结束返回语句并且返回类型不包括“未定义”

如何修复打字稿NodeJ中的eslint错误:“函数中缺少返回类型”

缺少函数的返回类型

Typescript 函数返回类型中继

array.map中缺少返回类型

registerReceiver中缺少该方法的返回类型

打字稿错误代码:2366,函数缺少结尾的return语句,并且返回类型不包含“ undefined”

TypeScript - 基于参数类型的函数返回类型

TypeScript推断类型构造函数中的回调返回类型

TypeScript:从函数中返回_.orderBy,要使用什么返回类型?

在没有 TypeScript 的情况下,这种类型注释如何在 React 代码中工作?

通过在主函数中添加“返回0”无法解决“缺少类型说明符-假定为int”

在函数中强制返回类型

Java中函数的返回类型

强制在Typescript中的类中返回所有函数的类型

根据TypeScript中的参数在字典中定义函数的返回类型

从异步函数销毁的对象中的类型在Typescript中返回

在 Typescript 中返回枚举成员时缺少隐式返回类型

Typescript从传递的函数返回类型推断返回类型

在 TypeScript 中将函数类型转换为这些函数的返回类型

Reduce函数上的几个Typescript(不能分配给类型的参数)错误

如何使用Material-UI按钮在Typescript函数上实现prop类型?

TypeScript如何在数组的Reduce函数上设置累积值和initialValue的类型