typescript-eslint: `@typescript-eslint/explicit-function-return-type` does not work in .js files, need preset config for JS files

Before You File a Proposal Please Confirm You Have Done The Following…

Description

@typescript-eslint/explicit-function-return-type is not a “type-checked” rule but could belong to the disableTypeChecked config since it does not make sense to require a return type in a context where types are not checked / mandatory, e.g. in .js files.

Impacted Configurations

disableTypeChecked config

Additional Info

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (15 by maintainers)

Most upvoted comments

@bradzacher, even myself who created this issue don’t see the need anymore. Moreover in the latest versions of eslint / typescript-eslint, existing shared configs don’t raise any error.

I would just disable all typescript-eslint rules.

I don’t think that’s a good summary, no. Many of our rules work just fine in JS files! Type-aware rules work just fine in JS files - so long as you’re declaring half-decent JSDoc types they’ll all work as expected. It’s just that JS files are often lazily typed (if at all) and the rules do essentially function on “garbage in, garbage out”.

IIRC there’s only three rules which will explicitly not work in a JS file:

  • explicit-function-return-type
  • explicit-module-boundary-types
  • parameter-properties

Everything else will work just fine (and the TS-specific stuff just never matches).

There could be some value in having a config to disable the above 3 rules as in “disable-requiring-type-syntax” or something? That way you could declare like

export default tseslint.config({
  files: ["**/*.js"],
  extends: [tseslint.configs.disableRequiringTypeSyntax, tseslint.configs.disableTypeChecked],
});

Hi @bradzacher, I refined the description.

Hi, not being able to find this issue, I unknowingly created a duplicate - so maybe this is some community reaction after all? 😄

My usecase is exactly as @bradzacher described - I have a fully TS type-aware codebase with a few config files (like eslint.config.js, but also other .config.js files for example) that are pure JS and I don’t really want to bother with typing them with jsdoc and looking for @types packages for them… But there are a lot of useful rules in typescript-eslint that I’d keep if I had the option… (see here for my example of a usecase)

With the new project service, it became easy to just use the default project with them and not bother configuring them totally separately, which is great. However, this makes some rules fail on them (it seems to me like with project service I don’t even need to disable type-aware rules at all?), like explicit-module-boundary-types, which I really like to use. To add to it, I can’t even do something like

{
  files: ['**/*.js'],
  ...tseslint.configs.disableTypeChecked,
  rules: {
    "@typescript-eslint/explicit-module-boundary-types": "off",
  },
},

because of how ES spreading works - I think I could just add 2 configs, but that’s a little bit confusing to us not that familiar with the new flat configs…

So I’d like to ask to reconsider the disable-things-that-wont-work-in-pure-js-files config because that’s exactly the solution I independently came up with when opening #9888…

Should we have a config like no-typescript-syntax-config that turns off all rules that add or modify TypeScript syntax…?

The issue is that the way the docs are currently written, is that it makes it seem like disable-type-checked is all that you need to do to get the setup working with js files

If you’re using the recommended config - then disable-type-checked is all you need 🙂

It’s only when people branch out into custom configs that they need more! For those custom usecases we do have it documented to some degree (docs that likely need updating 😅):

also, lol on the eslint.config.js being the culprit 😆