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
{{ message }}
Repository navigation
Commit 8546f77
Browse filesBrowse the repository at this point in the historyBrowse files
fix(no-unnecessary-type-assertion): don't flag array-to-tuple assertions; add preferConstToLiteralTuples
A non-`const` tuple `@type` on an array literal
(`/** @type {['foo']} */ (['foo'])`, or the leading-`@type` variant) is no
longer reported as redundant. The array literal only takes the tuple type
*from* the assertion (without it, it widens to `string[]`), so the assertion
is doing real work even though the contextually-typed expression echoes the
asserted tuple straight back. Handled in both the `VariableDeclaration` and
inline-cast paths.
Adds a default-off `preferConstToLiteralTuples` option: when enabled, such
assertions are instead reported (only when every tuple element is a literal)
with an autofix that rewrites the type to the equivalent, more concise
`/** @type {const} */`.
Fixes#1756
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Whether to report a non-`const` literal-tuple assertion on an array literal (e.g. `/** @type {['foo']} */ (['foo'])`) and fix it to the equivalent, more concise `/** @type {const} */` assertion. Defaults to `false`.
Copy file name to clipboardExpand all lines: src/rules.d.ts
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1324,6 +1324,10 @@ export interface Rules {
1324
1324
* Whether to enable the fixer that removes the redundant `@type` tag (and the JSDoc block if it becomes empty). Defaults to `true`.
1325
1325
*/
1326
1326
enableFixer?: boolean;
1327
+
/**
1328
+
* Whether to report a non-`const` literal-tuple assertion on an array literal (e.g. `/** @type {['foo']} * / (['foo'])`) and fix it to the equivalent, more concise `/** @type {const} * /` assertion. Defaults to `false`.
description: 'Whether to enable the fixer that removes the redundant `@type` tag (and the JSDoc block if it becomes empty). Defaults to `true`.',
504
585
type: 'boolean',
505
586
},
587
+
preferConstToLiteralTuples: {
588
+
description: 'Whether to report a non-`const` literal-tuple assertion on an array literal (e.g. `/** @type {[\'foo\']} */ ([\'foo\'])`) and fix it to the equivalent, more concise `/** @type {const} */` assertion. Defaults to `false`.',
589
+
type: 'boolean',
590
+
},
506
591
treatAnyAsRedundant: {
507
592
description: 'Whether to treat `any` type casts as redundant',
0 commit comments