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
453ac54e0a04d706a0631618fbd117d013e6fd713db8cdd5c4ab1f5f49713c86d842ec2734b7ad4fa56b3df00ed818808453aac78efd414005046087a6c943f2acfaab178a21e8af7cb5e1824059371e73eb89b6766a2e7854dcac0e489cb6534ef35474d2381b18e3164ae2b6bda8a3abfc236ff28c54da498eabd6b77dbbca206eec8883ffd2aae611ca15c4d0a3f5d74151fb67f4dc4334ccbc3ce111e1bfc2c5c1d7022bb891af5cfc05c3f37f60dcf36e6d81a92d242f0333d00808eebc8eb7aef6b5b91cbe41d8be852ca695407105b9f785c8626d16235acd3a1f38dd93fbcd83ad0e9e0bd76010136e5b386fd03ed1e07a3a89ec5346fdbc609351ea535fb68e3f91095bbaa55eae317751e2de3d465ccFile 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"}`) | ||
Contributor 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.
ContributorAuthor 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. Contributor 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.