@typescript-eslint/eslint-plugin 8.11.1-alpha.1 → 8.11.1-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -107,6 +107,47 @@ exports.default = (0, util_1.createRule)({
|
|
107
107
|
}
|
108
108
|
checkMembers(node.body.body, node, node.id, `type ${node.id.name}${genericTypes} = `, ';', !node.extends.length);
|
109
109
|
},
|
110
|
+
TSMappedType(node) {
|
111
|
+
const key = node.key;
|
112
|
+
const scope = context.sourceCode.getScope(key);
|
113
|
+
const scopeManagerKey = (0, util_1.nullThrows)(scope.variables.find(value => value.name === key.name && value.isTypeVariable), 'key type parameter must be a defined type variable in its scope');
|
114
|
+
// If the key is used to compute the value, we can't convert to a Record.
|
115
|
+
if (scopeManagerKey.references.some(reference => reference.isTypeReference)) {
|
116
|
+
return;
|
117
|
+
}
|
118
|
+
const constraint = node.constraint;
|
119
|
+
if (constraint.type === utils_1.AST_NODE_TYPES.TSTypeOperator &&
|
120
|
+
constraint.operator === 'keyof' &&
|
121
|
+
!(0, util_1.isParenthesized)(constraint, context.sourceCode)) {
|
122
|
+
// This is a weird special case, since modifiers are preserved by
|
123
|
+
// the mapped type, but not by the Record type. So this type is not,
|
124
|
+
// in general, equivalent to a Record type.
|
125
|
+
return;
|
126
|
+
}
|
127
|
+
// There's no builtin Mutable<T> type, so we can't autofix it really.
|
128
|
+
const canFix = node.readonly !== '-';
|
129
|
+
context.report({
|
130
|
+
node,
|
131
|
+
messageId: 'preferRecord',
|
132
|
+
...(canFix && {
|
133
|
+
fix: (fixer) => {
|
134
|
+
const keyType = context.sourceCode.getText(constraint);
|
135
|
+
const valueType = context.sourceCode.getText(node.typeAnnotation);
|
136
|
+
let recordText = `Record<${keyType}, ${valueType}>`;
|
137
|
+
if (node.optional === '+' || node.optional === true) {
|
138
|
+
recordText = `Partial<${recordText}>`;
|
139
|
+
}
|
140
|
+
else if (node.optional === '-') {
|
141
|
+
recordText = `Required<${recordText}>`;
|
142
|
+
}
|
143
|
+
if (node.readonly === '+' || node.readonly === true) {
|
144
|
+
recordText = `Readonly<${recordText}>`;
|
145
|
+
}
|
146
|
+
return fixer.replaceText(node, recordText);
|
147
|
+
},
|
148
|
+
}),
|
149
|
+
});
|
150
|
+
},
|
110
151
|
TSTypeLiteral(node) {
|
111
152
|
const parent = findParentDeclaration(node);
|
112
153
|
checkMembers(node.members, node, parent?.id, '', '');
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"consistent-indexed-object-style.js","sourceRoot":"","sources":["../../src/rules/consistent-indexed-object-style.ts"],"names":[],"mappings":";;
|
1
|
+
{"version":3,"file":"consistent-indexed-object-style.js","sourceRoot":"","sources":["../../src/rules/consistent-indexed-object-style.ts"],"names":[],"mappings":";;AAGA,oDAAoE;AAEpE,kCAAkE;AAKlE,kBAAe,IAAA,iBAAU,EAAsB;IAC7C,IAAI,EAAE,iCAAiC;IACvC,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EAAE,uCAAuC;YACpD,WAAW,EAAE,WAAW;SACzB;QACD,OAAO,EAAE,MAAM;QACf,QAAQ,EAAE;YACR,oBAAoB,EAAE,gDAAgD;YACtE,YAAY,EAAE,gDAAgD;SAC/D;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,WAAW,EAAE,wCAAwC;gBACrD,IAAI,EAAE,CAAC,QAAQ,EAAE,iBAAiB,CAAC;aACpC;SACF;KACF;IACD,cAAc,EAAE,CAAC,QAAQ,CAAC;IAC1B,MAAM,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC;QACpB,SAAS,YAAY,CACnB,OAA+B,EAC/B,IAA8D,EAC9D,QAAyC,EACzC,MAAc,EACd,OAAe,EACf,OAAO,GAAG,IAAI;YAEd,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACzB,OAAO;YACT,CAAC;YACD,MAAM,CAAC,M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CACzC,CAAC;gCAED,OAAO,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;4BAC7C,CAAC;yBACF,CAAC;qBACH,CAAC,CAAC;gBACL,CAAC;gBACD,aAAa,CAAC,IAAI;oBAChB,MAAM,MAAM,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAC;oBAC3C,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;gBACvD,CAAC;aACF,CAAC;SACH,CAAC;IACJ,CAAC;CACF,CAAC,CAAC;AAEH,SAAS,qBAAqB,CAC5B,IAAmB;IAEnB,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,sBAAc,CAAC,gBAAgB,EAAE,CAAC;QACxE,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,sBAAc,CAAC,sBAAsB,EAAE,CAAC;YAC/D,OAAO,IAAI,CAAC,MAAM,CAAC;QACrB,CAAC;QACD,OAAO,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
@@ -9,18 +9,24 @@ import TabItem from '@theme/TabItem';
|
|
9
9
|
>
|
10
10
|
> See **https://typescript-eslint.io/rules/consistent-indexed-object-style** for documentation.
|
11
11
|
|
12
|
-
TypeScript supports defining arbitrary object keys using an index signature
|
12
|
+
TypeScript supports defining arbitrary object keys using an index signature or mapped type.
|
13
|
+
TypeScript also has a builtin type named `Record` to create an empty object defining only an index signature.
|
14
|
+
For example, the following types are equal:
|
13
15
|
|
14
16
|
```ts
|
15
|
-
interface
|
17
|
+
interface IndexSignatureInterface {
|
16
18
|
[key: string]: unknown;
|
17
19
|
}
|
18
20
|
|
19
|
-
type
|
21
|
+
type IndexSignatureType = {
|
20
22
|
[key: string]: unknown;
|
21
23
|
};
|
22
24
|
|
23
|
-
type
|
25
|
+
type MappedType = {
|
26
|
+
[key in string]: unknown;
|
27
|
+
};
|
28
|
+
|
29
|
+
type RecordType = Record<string, unknown>;
|
24
30
|
```
|
25
31
|
|
26
32
|
Using one declaration form consistently improves code readability.
|
@@ -38,20 +44,24 @@ Using one declaration form consistently improves code readability.
|
|
38
44
|
<TabItem value="❌ Incorrect">
|
39
45
|
|
40
46
|
```ts option='"record"'
|
41
|
-
interface
|
47
|
+
interface IndexSignatureInterface {
|
42
48
|
[key: string]: unknown;
|
43
49
|
}
|
44
50
|
|
45
|
-
type
|
51
|
+
type IndexSignatureType = {
|
46
52
|
[key: string]: unknown;
|
47
53
|
};
|
54
|
+
|
55
|
+
type MappedType = {
|
56
|
+
[key in string]: unknown;
|
57
|
+
};
|
48
58
|
```
|
49
59
|
|
50
60
|
</TabItem>
|
51
61
|
<TabItem value="✅ Correct">
|
52
62
|
|
53
63
|
```ts option='"record"'
|
54
|
-
type
|
64
|
+
type RecordType = Record<string, unknown>;
|
55
65
|
```
|
56
66
|
|
57
67
|
</TabItem>
|
@@ -63,20 +73,24 @@ type Foo = Record<string, unknown>;
|
|
63
73
|
<TabItem value="❌ Incorrect">
|
64
74
|
|
65
75
|
```ts option='"index-signature"'
|
66
|
-
type
|
76
|
+
type RecordType = Record<string, unknown>;
|
67
77
|
```
|
68
78
|
|
69
79
|
</TabItem>
|
70
80
|
<TabItem value="✅ Correct">
|
71
81
|
|
72
82
|
```ts option='"index-signature"'
|
73
|
-
interface
|
83
|
+
interface IndexSignatureInterface {
|
74
84
|
[key: string]: unknown;
|
75
85
|
}
|
76
86
|
|
77
|
-
type
|
87
|
+
type IndexSignatureType = {
|
78
88
|
[key: string]: unknown;
|
79
89
|
};
|
90
|
+
|
91
|
+
type MappedType = {
|
92
|
+
[key in string]: unknown;
|
93
|
+
};
|
80
94
|
```
|
81
95
|
|
82
96
|
</TabItem>
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
3
|
-
"version": "8.11.1-alpha.
|
3
|
+
"version": "8.11.1-alpha.2",
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
5
5
|
"files": [
|
6
6
|
"dist",
|
@@ -61,10 +61,10 @@
|
|
61
61
|
},
|
62
62
|
"dependencies": {
|
63
63
|
"@eslint-community/regexpp": "^4.10.0",
|
64
|
-
"@typescript-eslint/scope-manager": "8.11.1-alpha.
|
65
|
-
"@typescript-eslint/type-utils": "8.11.1-alpha.
|
66
|
-
"@typescript-eslint/utils": "8.11.1-alpha.
|
67
|
-
"@typescript-eslint/visitor-keys": "8.11.1-alpha.
|
64
|
+
"@typescript-eslint/scope-manager": "8.11.1-alpha.2",
|
65
|
+
"@typescript-eslint/type-utils": "8.11.1-alpha.2",
|
66
|
+
"@typescript-eslint/utils": "8.11.1-alpha.2",
|
67
|
+
"@typescript-eslint/visitor-keys": "8.11.1-alpha.2",
|
68
68
|
"graphemer": "^1.4.0",
|
69
69
|
"ignore": "^5.3.1",
|
70
70
|
"natural-compare": "^1.4.0",
|
@@ -75,8 +75,8 @@
|
|
75
75
|
"@types/marked": "^5.0.2",
|
76
76
|
"@types/mdast": "^4.0.3",
|
77
77
|
"@types/natural-compare": "*",
|
78
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "8.11.1-alpha.
|
79
|
-
"@typescript-eslint/rule-tester": "8.11.1-alpha.
|
78
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "8.11.1-alpha.2",
|
79
|
+
"@typescript-eslint/rule-tester": "8.11.1-alpha.2",
|
80
80
|
"ajv": "^6.12.6",
|
81
81
|
"cross-env": "^7.0.3",
|
82
82
|
"cross-fetch": "*",
|