-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
@typescript-eslint/explicit-function-return-type
does not work in .js files, need preset config for JS files
#8955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Closing this issue after @bradzacher closed the related PR. |
@OlivierZal we can still discuss this idea here! We just like to start with an issue so we don't put the cart before the horse and have you burn time on work that might not be accepted. |
Hi @bradzacher, I refined the description. |
TLDR - I don't think you want to use disable-type-checked for disabling typescript-eslint rules in I guess it's a question of what the purpose of the disable-type-checked config is. The example given in the docs shows it as being used to disable type-checked rules for To me, that seems wrong. Granted our "New Rule" issue template says "My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.", if a rule
So, there should be no scenario where you'd only want typed lint rules disabled from JS files, right? So, I come back to - that seems like a wrong use of the disable-type-checked config. I'd think instead, you'd simply... not want to enable typescript-eslint rules in the first place in a JS file. And disable-type-checked would be used for TS files you might have in your repo that you can't/don't want to set up with typed linting, or for removing type-aware rules out of a config that you've imported from somewhere else. @bradzacher does that align with your understanding? |
I don't think that's a good summary, no. IIRC there's only three rules which will explicitly not work in a JS file:
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],
}); |
Lol, savage 😅 But, after reading your response, I think you're right 😆
This is actually very cool! I haven't used the jsdoc-typed-js workflow yet, so I learned something new today. I think this does prove the point that using
Looking around a bit, typedef should probably go in there too, since that's all about adding type annotations. Didn't find any other obvious examples.
+1 Can we take an action item to change the docs to not give And we can split out the idea of |
The reason we use this as our example is because in modern TS codebases that's what most users want to do! Having The important bit is that because of their nature these files are rarely distributed to users and so people don't bother trying to create a tsconfig setup that matches those files. So the common user story as a modern TS codebase is "I have JS files for config but I don't want to try and make them work with type-aware linting so I want to disable type-aware linting altogether". But ofc there are some codebases that are all-in on JS with JSDoc plus TS and type-aware linting. So in those codebases the above story does not apply. These codebases are the very rare case though cos jsdoc typing is cumbersome AF. Its worth noting that with the new project service we've added better support for this usecase as these files can be automatically covered by a "default program". But it's still likely that people don't want type-aware rules because JSDoc typing is cumbersome AF and often times you haven't bothered grabbing |
I totally agree: I have only TS files... except the eslint flat config, which as far as I know must be a file named exactly |
And, fair enough, but wouldn't it more helpful to the user to achieve that goal having a doc section that looks (very very) roughly like Working with a mixed JS/TS codebaseMany of our rules actually work just fine in JS files! However, some of our rules will cause spurious errors if they're applied to JS files, so you'll need to disable them. export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigDirName: import.meta.dirname,
},
},
},
+ {
+ // These rules will try to enforce TS syntax which is illegal inside a JS file
+ // maybe one day this will be included in a utility config rather than listed out individually
+ files: ['**/*.js'],
+ rules: [
+ "@typescript-eslint/explicit-function-return-types": "off",
+ "@typescript-eslint/parameter-properties": "off",
+ "@typescript-eslint/explicit-module-boundary-types": "off",
+ "@typescript-eslint/typedef": "off",
+ ]
+ },
); Depending on your setup, our type-aware rules may cause problems as well. We provide the utility disable-type-checked config to help you disable them too export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigDirName: import.meta.dirname,
},
},
},
{
// These rules will try to enforce TS syntax which is illegal inside a JS file
files: ['**/*.js'],
rules: [
"@typescript-eslint/explicit-function-return-types": "off",
"@typescript-eslint/parameter-properties": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/typedef": "off",
]
},
+ {
+ files: ['**/*.js'],
+ ...tseslint.configs.disableTypeChecked,
+ }
); Alternately, if you use JS doc types in your JS files (helpful-link-to-more-info-on-this-for-people-who-don't-know-what-that-is), our type-aware rules will work, as long as the corresponding tsconfig is included in the eslint config. 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, which is how we got to this issue report in the first place. |
also, lol on the eslint.config.js being the culprit 😆 |
If you're using the recommended config - then It's only when people branch out into custom configs that they need more! |
Should we have a config like |
@typescript-eslint/explicit-function-return-type
rule@typescript-eslint/explicit-function-return-type
does not work in .js files, need preset config for JS files
4 months later and this request has received 0 community reactions. |
@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. |
Hi, My usecase is exactly as @bradzacher described - I have a fully TS type-aware codebase with a few config files (like 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 {
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 |
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
The text was updated successfully, but these errors were encountered: