@typescript-eslint/eslint-plugin 8.30.2-alpha.5 → 8.30.2-alpha.6

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.
@@ -1,5 +1,6 @@
1
1
  export type Options = [
2
2
  {
3
+ checkLiteralConstAssertions?: boolean;
3
4
  typesToIgnore?: string[];
4
5
  }
5
6
  ];
@@ -1 +1 @@
1
- {"version":3,"file":"no-unnecessary-type-assertion.d.ts","sourceRoot":"","sources":["../../src/rules/no-unnecessary-type-assertion.ts"],"names":[],"mappings":"AAqBA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;KAC1B;CACF,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,yBAAyB,GAAG,sBAAsB,CAAC;;AAE5E,wBAwXG"}
1
+ {"version":3,"file":"no-unnecessary-type-assertion.d.ts","sourceRoot":"","sources":["../../src/rules/no-unnecessary-type-assertion.ts"],"names":[],"mappings":"AAqBA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,2BAA2B,CAAC,EAAE,OAAO,CAAC;QACtC,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;KAC1B;CACF,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,yBAAyB,GAAG,sBAAsB,CAAC;;AAE5E,wBA4YG"}
@@ -56,6 +56,10 @@ exports.default = (0, util_1.createRule)({
56
56
  type: 'object',
57
57
  additionalProperties: false,
58
58
  properties: {
59
+ checkLiteralConstAssertions: {
60
+ type: 'boolean',
61
+ description: 'Whether to check literal const assertions.',
62
+ },
59
63
  typesToIgnore: {
60
64
  type: 'array',
61
65
  description: 'A list of type names to ignore.',
@@ -180,17 +184,27 @@ exports.default = (0, util_1.createRule)({
180
184
  }
181
185
  return false;
182
186
  }
187
+ function isTypeLiteral(type) {
188
+ return type.isLiteral() || tsutils.isBooleanLiteralType(type);
189
+ }
183
190
  return {
184
191
  'TSAsExpression, TSTypeAssertion'(node) {
185
192
  if (options.typesToIgnore?.includes(context.sourceCode.getText(node.typeAnnotation))) {
186
193
  return;
187
194
  }
188
195
  const castType = services.getTypeAtLocation(node);
196
+ const castTypeIsLiteral = isTypeLiteral(castType);
197
+ const typeAnnotationIsConstAssertion = isConstAssertion(node.typeAnnotation);
198
+ if (!options.checkLiteralConstAssertions &&
199
+ castTypeIsLiteral &&
200
+ typeAnnotationIsConstAssertion) {
201
+ return;
202
+ }
189
203
  const uncastType = services.getTypeAtLocation(node.expression);
190
204
  const typeIsUnchanged = isTypeUnchanged(uncastType, castType);
191
- const wouldSameTypeBeInferred = castType.isLiteral()
205
+ const wouldSameTypeBeInferred = castTypeIsLiteral
192
206
  ? isImplicitlyNarrowedLiteralDeclaration(node)
193
- : !isConstAssertion(node.typeAnnotation);
207
+ : !typeAnnotationIsConstAssertion;
194
208
  if (typeIsUnchanged && wouldSameTypeBeInferred) {
195
209
  context.report({
196
210
  node,
@@ -37,10 +37,6 @@ type Foo = number;
37
37
  const foo = (3 + 5) as Foo;
38
38
  ```
39
39
 
40
- ```ts
41
- const foo = 'foo' as const;
42
- ```
43
-
44
40
  ```ts
45
41
  function foo(x: number): number {
46
42
  return x!; // unnecessary non-null
@@ -73,6 +69,18 @@ function foo(x: number | undefined): number {
73
69
 
74
70
  ## Options
75
71
 
72
+ ### `checkLiteralConstAssertions`
73
+
74
+ {/* insert option description */}
75
+
76
+ With `@typescript-eslint/no-unnecessary-type-assertion: ["error", { checkLiteralConstAssertions: true }]`, the following is **incorrect** code:
77
+
78
+ ```ts option='{ "checkLiteralConstAssertions": true }' showPlaygroundButton
79
+ const foo = 'foo' as const;
80
+ ```
81
+
82
+ See [#8721 False positives for "as const" assertions (issue comment)](https://github.com/typescript-eslint/typescript-eslint/issues/8721#issuecomment-2145291966) for more information on this option.
83
+
76
84
  ### `typesToIgnore`
77
85
 
78
86
  {/* insert option description */}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.30.2-alpha.5",
3
+ "version": "8.30.2-alpha.6",
4
4
  "description": "TypeScript plugin for ESLint",
5
5
  "files": [
6
6
  "dist",
@@ -62,10 +62,10 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "@eslint-community/regexpp": "^4.10.0",
65
- "@typescript-eslint/scope-manager": "8.30.2-alpha.5",
66
- "@typescript-eslint/type-utils": "8.30.2-alpha.5",
67
- "@typescript-eslint/utils": "8.30.2-alpha.5",
68
- "@typescript-eslint/visitor-keys": "8.30.2-alpha.5",
65
+ "@typescript-eslint/scope-manager": "8.30.2-alpha.6",
66
+ "@typescript-eslint/type-utils": "8.30.2-alpha.6",
67
+ "@typescript-eslint/utils": "8.30.2-alpha.6",
68
+ "@typescript-eslint/visitor-keys": "8.30.2-alpha.6",
69
69
  "graphemer": "^1.4.0",
70
70
  "ignore": "^5.3.1",
71
71
  "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.30.2-alpha.5",
79
- "@typescript-eslint/rule-tester": "8.30.2-alpha.5",
78
+ "@typescript-eslint/rule-schema-to-typescript-types": "8.30.2-alpha.6",
79
+ "@typescript-eslint/rule-tester": "8.30.2-alpha.6",
80
80
  "@vitest/coverage-v8": "^3.1.1",
81
81
  "ajv": "^6.12.6",
82
82
  "cross-fetch": "*",