From 6320bb39ac30207a27553687864005085a452781 Mon Sep 17 00:00:00 2001 From: Ronen Amiel Date: Thu, 6 Feb 2025 17:57:08 +0200 Subject: [PATCH] add missing test for casting enums for no-unnecessary-type-assertion --- .../no-unnecessary-type-assertion.test.ts | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts index a686238bad95..1ef1a3dfb371 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts @@ -384,6 +384,38 @@ if (Math.random()) { x!; `, }, + { + code: ` +enum T { + Value1, + Value2, +} + +declare const a: T.Value1; +const b = a as T.Value2; + `, + }, + { + code: ` +enum T { + Value1, + Value2, +} + +declare const a: T.Value1; +const b = a as T; + `, + }, + { + code: ` +enum T { + Value1 = 0, + Value2 = 1, +} + +const b = 1 as T.Value2; + `, + }, ], invalid: [ @@ -1253,5 +1285,55 @@ const a = ''; const b: string | undefined = (a ? undefined : a); `, }, + { + code: ` +enum T { + Value1, + Value2, +} + +declare const a: T.Value1; +const b = a as T.Value1; + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + }, + ], + output: ` +enum T { + Value1, + Value2, +} + +declare const a: T.Value1; +const b = a; + `, + }, + { + code: ` +enum T { + Value1, + Value2, +} + +declare const a: T.Value1; +const b = a as const; + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + }, + ], + output: ` +enum T { + Value1, + Value2, +} + +declare const a: T.Value1; +const b = a; + `, + }, ], });