-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[prefer-readonly-parameter-types] inferred types from third party code make rule painful #2079
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
I believe that the reason that If they had instead used I don't think it's unreasonable to add an option to ignore functions in a callback position. |
I'm not sure how much api is available for this but something like the following would solve most common use cases I think. global.d.ts// Just an alias to the original type
/**
* Explicitly mark a type T as allowed for `prefer-readonly-parameter-types`
* ⚠ `Only use it when you know what you're doing` <- Or some similar warning text
*/
declare type Mutable<T = unknown> = T; eslintrc.yaml '@typescript-eslint/prefer-readonly-parameter-types':
- error
# Either
- allowedTypes: ['Mutable<*>'] # Regex/Glob pattern to exclude
# Or
- allowedTypes: ['./global.d.ts#Mutable'] # Full path to the type itself relative to the config file
# Or
- allowedTypes: ['io-ts', 'package-name', 'package-name/specific/type/import/path'] # Package name or path to package specific type as it would be if it was imported
# Or
- allowThirdParty: true # Any type that is not declared within the tsconfig project src/root being linted test.tsimport t from io-ts;
// No errors here
const parseValidationError = (error: Mutable<t.ValidationError>) => {
return error;
}
// Or here
const parseValidationError = (error: t.ValidationError) => {
return error;
} The above
+1 For that as well |
I'm setting up a project and... this is indeed painful. So much so that I'm likely to disable it and accept mutation risks that I'd like to avoid. The discussion here is interesting, but...
I'm not sure this is the best solution. What I would want is:
Given the prevalence of mutable types even in libraries that strongly focus on pure functional patterns (the |
There's always the option to contribute fixes upstream? Happy to accept a PR to add a new option to ignore parameters without an explicit type annotation. As with almost everything in this project - this rule is maintained by the community. The best and fastest way to see a change done is to roll up your sleeve and contribute! This is a pointer to roughly where you'd need to start making a change: typescript-eslint/packages/eslint-plugin/src/rules/prefer-readonly-parameter-types.ts Lines 73 to 83 in 03886d7
|
@bradzacher thanks! I'll take a look and see if I can make some progress on the change, I'd be happy to contribute. |
At this state I would call it fixed by #2668 since it seems the only agreement reached is to "ignore parameters without an explicit type annotation", which |
i like the concept of the checker, but it's a pain in the butt in practice if you use third party libraries with typings and hope to use typescript's inference engine, because it errors like crazy.
Repro
Install one of many popular libraries:
yarn add -D ava # or maybe yarn add io-ts
or maybe
Expected Result
unknown
values inio-ts
are essential to its usage, and I trust it, so I'm OK with letting anything fromio-ts
slideActual Result
Errors that I can't fix since it's not my typings, besides just ignoring the rule or turning it off
Versions
@typescript-eslint/eslint-plugin
2.29.0
@typescript-eslint/parser
2.29.0
TypeScript
3.9.3
ESLint
6.8.0
node
12.16.3
npm
6.14.4
The text was updated successfully, but these errors were encountered: