打字稿|关于函数ESLint的缺少返回类型的警告

时间:2019-02-21 19:28:09

标签: javascript reactjs typescript types eslint

在具有TypeScript的项目中,我有一个library(purrr) library(magick) capturas <- list.files("./path/to/images/", pattern = "\\.png$") # get all images in a list images <- map(capturas, image_read) images <- image_join(images) image_animate(images, fps = 1, dispose = "previous") 。它有一个错误,说

REACT-STATELESS-COMPONENT

我不确定它要我做什么。这是我的代码:

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

你能告诉我我需要做什么。我需要阅读有关TS的信息,但目前我根本不了解它。而且我现在不能这样做。

3 个答案:

答案 0 :(得分:6)

我建议使用react提供的类型;它们将包含返回类型。如果您使用的是最新的React版本(16.8.0或更高版本),请执行以下操作:

var ctx = document.getElementById("my3Chart");

var mylineChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Time Management", "Career Coach", "Stress & Wellbeing", "Note Taking", "Exam Prep", "Presentations"],
        datasets: [{
            label: 'Module Tracker',
            data: [6, 4, 2, 0, 3, 1],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

在16.8之前,您应该这样做:

const Component: React.FunctionComponent<Props> = (props: Props) => (

SFC代表“无状态功能组件”。他们更改了名称,因为功能组件不再必须是无状态的。

答案 1 :(得分:3)

对于函数返回类型,它位于参数之后:

({ prop }: Props & T): JSX.Element => {}

JSX.Element是TypeScript的推断,这是一个非常安全的选择。

如果您有好奇心,应该可以将TypeScript悬停在Component上,以查看TypeScript推断为返回类型,这将显示整个签名。

答案 2 :(得分:0)

这是我通常使用打字稿声明comp的方式:

  import * as React from 'react';

    type MyComponentProps = {
    myStringProp: String,
    myOtherStringProp: String
    };

    const MyComponent: React.FunctionComponent<MyComponentProps> = ({myStringProp, myOtherStringProp}): JSX.Element => {
        return (
            <div>
                <h1>This is My Component</h1>
            </div>
        );
    };


    export default MyComponent;
相关问题