diff --git a/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json b/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json new file mode 100644 index 00000000000..7ce436c2107 --- /dev/null +++ b/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/eslint-plugin", + "comment": "Allow using `as const` in `typedef-var`", + "type": "minor" + } + ], + "packageName": "@rushstack/eslint-plugin" +} \ No newline at end of file diff --git a/eslint/eslint-plugin/src/test/typedef-var.test.ts b/eslint/eslint-plugin/src/test/typedef-var.test.ts index fb12ffe09ff..a873952faae 100644 --- a/eslint/eslint-plugin/src/test/typedef-var.test.ts +++ b/eslint/eslint-plugin/src/test/typedef-var.test.ts @@ -46,6 +46,15 @@ ruleTester.run('typedef-var', typedefVar, { { code: 'for (const x of []) { }' }, + { + code: 'const x = 1 as const;' + }, + { + code: 'const x: 1 = 1;' + }, + { + code: 'const x: number = 1;' + }, { // prettier-ignore code: [ diff --git a/eslint/eslint-plugin/src/typedef-var.ts b/eslint/eslint-plugin/src/typedef-var.ts index 33ed7cd1f57..6202d4d2844 100644 --- a/eslint/eslint-plugin/src/typedef-var.ts +++ b/eslint/eslint-plugin/src/typedef-var.ts @@ -50,6 +50,16 @@ const typedefVar: TSESLint.RuleModule = { return; } + if ( + node.init?.type === AST_NODE_TYPES.TSAsExpression && + node.init.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference && + node.init.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier && + node.init.typeAnnotation.typeName.name === 'const' + ) { + // An `as const` type declaration was provided + return; + } + // These are @typescript-eslint/typedef exemptions if ( node.id.type === AST_NODE_TYPES.ArrayPattern /* ArrayDestructuring */ ||