Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [prefer-readonly-parameter-types] added an optional type allowlist#4436
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
453ac54
e0a04d7
06a0631
618fbd1
17d013e
6fd713d
b8cdd5c
4ab1f5f
49713c8
6d842ec
2734b7a
d4fa56b
3df00ed
8188084
53aac78
efd4140
0504608
7a6c943
f2acfaa
b178a21
e8af7cb
5e18240
59371e7
3eb89b6
766a2e7
854dcac
0e489cb
6534ef3
5474d23
81b18e3
164ae2b
6bda8a3
abfc236
ff28c54
da498ea
bd6b77d
bbca206
eec8883
ffd2aae
611ca15
c4d0a3f
5d74151
fb67f4d
c4334cc
bc3ce11
1e1bfc2
c5c1d70
22bb891
af5cfc0
5c3f37f
60dcf36
e6d81a9
2d242f0
333d008
08eebc8
eb7aef6
b5b91cb
e41d8be
852ca69
5407105
b9f785c
8626d16
235acd3
a1f38dd
93fbcd8
3ad0e9e
0bd7601
0136e5b
386fd03
ed1e07a
3a89ec5
346fdbc
609351e
a535fb6
8e3f910
95bbaa5
5eae317
751e2de
3d465cc
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -129,6 +129,101 @@ interface Foo { | ||
## Options | ||
### `allow` | ||
Some complex types cannot easily be made readonly, for example the `HTMLElement` type or the `JQueryStatic` type from `@types/jquery`. This option allows you to globally disable reporting of such types. | ||
Each item must be one of: | ||
- A type defined in a file (`{from: "file", name: "Foo", path: "src/foo-file.ts"}` with `path` being an optional path relative to the project root directory) | ||
- A type from the default library (`{from: "lib", name: "Foo"}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Ahh, that's were all the conversation about the details is. 🙂 | ||
- A type from a package (`{from: "package", name: "Foo", package: "foo-lib"}`, this also works for types defined in a typings package). | ||
Additionally, a type may be defined just as a simple string, which then matches the type independently of its origin. | ||
Examples of code for this rule with: | ||
```json | ||
{ | ||
"allow": [ | ||
"$", | ||
{ "source": "file", "name": "Foo" }, | ||
{ "source": "lib", "name": "HTMLElement" }, | ||
{ "from": "package", "name": "Bar", "package": "bar-lib" } | ||
] | ||
} | ||
``` | ||
<!--tabs--> | ||
#### ❌ Incorrect | ||
```ts | ||
interface ThisIsMutable { | ||
prop: string; | ||
} | ||
interface Wrapper { | ||
sub: ThisIsMutable; | ||
} | ||
interface WrapperWithOther { | ||
readonly sub: Foo; | ||
otherProp: string; | ||
} | ||
function fn1(arg: ThisIsMutable) {} // Incorrect because ThisIsMutable is not readonly | ||
function fn2(arg: Wrapper) {} // Incorrect because Wrapper.sub is not readonly | ||
function fn3(arg: WrapperWithOther) {} // Incorrect because WrapperWithOther.otherProp is not readonly and not in the allowlist | ||
``` | ||
```ts | ||
import { Foo } from 'some-lib'; | ||
import { Bar } from 'incorrect-lib'; | ||
interface HTMLElement { | ||
prop: string; | ||
} | ||
function fn1(arg: Foo) {} // Incorrect because Foo is not a local type | ||
function fn2(arg: HTMLElement) {} // Incorrect because HTMLElement is not from the default library | ||
function fn3(arg: Bar) {} // Incorrect because Bar is not from "bar-lib" | ||
``` | ||
#### ✅ Correct | ||
```ts | ||
interface Foo { | ||
prop: string; | ||
} | ||
interface Wrapper { | ||
readonly sub: Foo; | ||
readonly otherProp: string; | ||
} | ||
function fn1(arg: Foo) {} // Works because Foo is allowed | ||
function fn2(arg: Wrapper) {} // Works even when Foo is nested somewhere in the type, with other properties still being checked | ||
``` | ||
```ts | ||
import { Bar } from 'bar-lib'; | ||
interface Foo { | ||
prop: string; | ||
} | ||
function fn1(arg: Foo) {} // Works because Foo is a local type | ||
function fn2(arg: HTMLElement) {} // Works because HTMLElement is from the default library | ||
function fn3(arg: Bar) {} // Works because Bar is from "bar-lib" | ||
``` | ||
```ts | ||
import { Foo } from './foo'; | ||
function fn(arg: Foo) {} // Works because Foo is still a local type - it has to be in the same package | ||
``` | ||
### `checkParameterProperties` | ||
This option allows you to enable or disable the checking of parameter properties. | ||
Uh oh!
There was an error while loading.Please reload this page.