我应该如何修复 "Expected to return a value at the end of arrow function."?

How should I fix "Expected to return a value at the end of arrow function."?

我正在使用 typescripteslint。 Eslint 在 => 箭头之后抱怨 return,当我添加它时这也不起作用 - return new Promise((resolve, reject) => return {}。 -

的正确语法是什么
function getSizeFromObjectUrl(dataURL: string): Promise<any> {
    return new Promise((resolve, reject) => {
        try {
            const img = new Image();
            img.onload = () => {
                const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
                return resolve({
                    height: img.height * ratio,
                    width: img.width * ratio
                });
            };
            img.src = dataURL;
        } catch (exception) {
            return reject(exception);
        }
    });
}

像-

一样使用它

const size = await getSizeFromObjectUrl(imageUrl);

正确的语法是:

function getSizeFromObjectUrl(dataURL: string): Promise<any> {
    return new Promise((resolve, reject) => {
        try {
            const img = new Image();
            img.onload = () => {
                const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
                resolve({
                    height: img.height * ratio,
                    width: img.width * ratio
                });
            };
            img.src = dataURL;
        } catch (exception) {
            reject(exception);
        }
    });
}

它实际上是在抱怨 return before resolve / reject not after the arrow。因为 resolve 和 reject 函数是空的

对于错误 Unexpected lexical declaration in case block.,使用这样的案例:

case x: {
    // your code goes here
}

而不是:

case x: 
    // your code

我在 promise resolve/reject 之前删除了 return。这有效-

function getSizeFromObjectUrl(dataURL: string): Promise<any> {
    return new Promise((resolve, reject) => {
        try {
            const img = new Image();
            img.onload = () => {
                const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
                resolve({
                    height: img.height * ratio,
                    width: img.width * ratio
                });
            };
            img.src = dataURL;
        } catch (exception) {
            reject(exception);
        }
    });
}

规则大致一致return:https://eslint.org/docs/rules/consistent-return

A confusing aspect of JavaScript is that a function returns undefined if any of the following are true:

  • it does not execute a return statement before it exits
  • it executes return which does not specify a value explicitly
  • it executes return undefined
  • it executes return void followed by an expression (for example, a function call)
  • it executes return followed by any other expression which evaluates to undefined

If any code paths in a function return a value explicitly but some code path do not return a value explicitly, it might be a typing mistake, especially in a large function.

因此,您需要做的就是删除该消息:

  • 明确 return try 块中的内容(return undefined 如果你愿意的话)
  • catch 块中停止 returning
  • 禁用该功能的规则