Repository: edvardchen/eslint-plugin-i18next (js)

Issue:
Avoid detecting text of type union.
We would like to prevent Lint from detecting values with the union type specified, as shown below.
Currently, it is detected as an error.

```ts
type Animal = 'dog' | 'cat'

const animal: Animal = 'dog' -> disallow literal string: sample: Animal = 'dog'eslint(i18next/no-literal-string)
```

---

This PR https://github.com/edvardchen/eslint-plugin-i18next/pull/56 has been deleted in what appears to be the relevant code. Do you plan to implement similar changes again?

Pull request description:
feat: ignore hand-written literal type
Fixes #114 

Patch:
diff --git a/lib/rules/no-literal-string.js b/lib/rules/no-literal-string.js
index 057dd24..3565176 100644
--- a/lib/rules/no-literal-string.js
+++ b/lib/rules/no-literal-string.js
@@ -89,10 +89,39 @@ module.exports = {
       });
     }
 
+    const { esTreeNodeToTSNodeMap, program } = parserServices;
+    let typeChecker;
+    if (program && esTreeNodeToTSNodeMap)
+      typeChecker = program.getTypeChecker();
+
     function validateBeforeReport(node) {
       if (isValidScope()) return;
       if (isValidLiteral(options, node)) return;
 
+      // ─── Typescript ──────────────────────────────────────
+
+      if (typeChecker) {
+        const tsNode = esTreeNodeToTSNodeMap.get(node);
+        const typeObj = typeChecker.getContextualType(tsNode);
+        if (typeObj) {
+          // var a: 'abc' = 'abc'
+          if (typeObj.isStringLiteral()) {
+            return;
+          }
+
+          // var a: 'abc' | 'name' = 'abc'
+          if (typeObj.isUnion()) {
+            const found = typeObj.types.some(item => {
+              if (item.isStringLiteral() && item.value === node.value) {
+                return true;
+              }
+            });
+            if (found) return;
+          }
+        }
+      }
+      // ─────────────────────────────────────────────────────
+
       report(node);
     }
 


Test patch:
diff --git a/tests/lib/fixtures/valid-typescript.ts b/tests/lib/fixtures/valid-typescript.ts
index 552d691..4821c3b 100644
--- a/tests/lib/fixtures/valid-typescript.ts
+++ b/tests/lib/fixtures/valid-typescript.ts
@@ -18,4 +18,11 @@ var a6 = T2['howard'];
 function Button2({ t = 'name' }: { t: 'name' }) {}
 
 type Ta = { t?: 'name' | 'abc' };
+let ta: Ta = { t: 'abc' };
+
+const str: 'str' = 'str';
+
+type Animal = 'dog' | 'cat';
+const animal: Animal = 'dog';
+
 function Button3({ t = 'name' }: Ta) {}