You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rules/no-unused-properties.md
+19-2Lines changed: 19 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ This rule is primarily useful when you use objects to group constants or model e
13
13
14
14
## Example use cases
15
15
16
-
When using [React](https://reactjs.org)'s inline styles or one of [many CSS-in-JS](https://michelebertoli.github.io/css-in-js/) like [glamor](https://github.com/threepointone/glamor), one might find it helpful to group component styles into a constant object. Later you might remove one of the styles, but forget to remove its definition, especially if the component grew in complexity by that time. If these were defined as separate constants, ESLint's builtin`no-unused-vars` rule would have helped, but they are not. That's when the `no-unused-properties`rules becomes useful.
16
+
When using [React](https://reactjs.org)'s inline styles or one of [many CSS-in-JS](https://michelebertoli.github.io/css-in-js/)libraries like [glamor](https://github.com/threepointone/glamor), one might find it helpful to group component styles into a constant object. Later you might remove one of the styles, but forget to remove its definition, especially if the component grew in complexity by that time. If these were defined as separate constants, ESLint's built-in`no-unused-vars` rule would have helped, but they are not. That's when the `no-unused-properties`rule becomes useful.
17
17
18
18
```js
19
19
conststyles= {
@@ -48,6 +48,22 @@ console.log(myEnum.used);
48
48
const {used} = myEnum;
49
49
```
50
50
51
+
This rule also checks inline TypeScript object type literals attached directly to initialized variables and parameters.
52
+
53
+
```ts
54
+
// ❌
55
+
function foo(args: {used:number; unused:number}) {
56
+
returnargs.used;
57
+
}
58
+
```
59
+
60
+
```ts
61
+
// ✅
62
+
function foo(args: {used:number}) {
63
+
returnargs.used;
64
+
}
65
+
```
66
+
51
67
```js
52
68
// ✅
53
69
constmyEnum= {
@@ -85,7 +101,7 @@ const foo = {
85
101
86
102
This rule tries hard not to report potentially used properties as unused. Basically, all supported use cases are enum-like as shown above, more complex cases are ignored. Detailed list of limitations follows.
87
103
88
-
- Does not predict dynamic property access. That is, `object[keys]` says that all properties of an object are potentially used. This is as unpredictable for this rule as `eval(...)` is for the `no-unused-vars` rule.
104
+
- Does not predict dynamic property access. That is, `object[key]` says that all properties of an object are potentially used. This is as unpredictable for this rule as `eval(...)` is for the `no-unused-vars` rule.
89
105
- Same goes for computed property keys in object definitions, like `{[key]: value}`.
90
106
- If a variable is unused, it is not checked. This is done to play nicely with the `no-unused-vars` rule, which everybody should have enabled at all times.
91
107
- Does not check objects used as an argument. If you call `f(object)`, it behaves like you used all of its properties, since it's hard to predict what a function would do with the object.
@@ -94,5 +110,6 @@ This rule tries hard not to report potentially used properties as unused. Basica
94
110
- Classes are not checked.
95
111
- Prototypes are not checked. Only own properties are.
96
112
- Does not follow objects across files. If you export an object, it's like if you used all of its properties.
113
+
- TypeScript support is limited to inline object type literals attached directly to initialized variables and parameters. Named type aliases, interfaces, and destructured typed bindings are not followed.
97
114
98
115
If you want to lift some of these limitations, you should try tools like [Flow](https://flow.org) or [TypeScript](https://www.typescriptlang.org).
0 commit comments