denoland / deno_lint

Blazing fast linter for JavaScript and TypeScript written in Rust
https://lint.deno.land/
MIT License
1.51k stars 163 forks source link

Disable `require-await` by default #1170

Open not-my-profile opened 1 year ago

not-my-profile commented 1 year ago

Consider the following code:

interface Foo {
  bar(): Promise<number>;
}

class Bar implements Foo {
  async bar() {
    return 1;
  }
}

deno lint currently by default complains:

(require-await) Async method 'bar' has no 'await' expression.
  async bar() {
        ^^^
    at /tmp/foo.ts:6:9

    hint: Remove 'async' keyword from the function or use 'await' expression inside.
    help: for further information visit https://lint.deno.land/#require-await

However if you follow that hint and remove the async keyword you actually introduce the following TypeScript error (as reported by deno check):

error: TS2416 [ERROR]: Property 'bar' in type 'Bar' is not assignable to the same property in base type 'Foo'.
  Type '() => number' is not assignable to type '() => Promise<number>'.
    Type 'number' is not assignable to type 'Promise<number>'.
  bar() {
  ~~~

Since implementing some interface that requires an async function is incredibly common in TypeScript and deno lint currently has absolutely no clue about types (see #1138) and therefore currently cannot detect that the async keyword is actually needed, I think it would be best to disable the require-await rule by default (i.e. removing it from the recommended rules set).

(sidenote: ESLint also doesn't recommend require-await)

yacinehmito commented 11 months ago

I support this suggestion. Disabling this rule is the first thing I do when I setup a project with Deno. It's super annoying.

jordanbtucker commented 10 months ago

Alternatively, the rule could catch situations like these and not report them as errors. Although, that might require type checking.

karfau commented 2 months ago

There is also https://typescript-eslint.io/rules/promise-function-async/ which makes so much sense to me. What is it about type checking, is it not possible for deno to do that? Or asked differently, is there any chance that deno will ever have a rule like promise-function-async?

(And with the background of JSR "performant types" in which, if I understood it correctly, all exported functions have to declare their return type, maybe it would even be possible to that without type checks, by enforcing declaring the return type on all functions?)