typescript-eslint: [no-unused-vars] False positives with members of exported namespace in .d.ts file

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "rules": {
    "@typescript-eslint/no-unused-vars": "error"
  }
}
// foo.d.ts
export namespace Foo {
  const bar: string;
  namespace Baz {
    const quux: string;
  }
}

Expected Result

No error. In a .d.ts file, members of an exported namespace are implicitly exported, unless the namespace contains an explicit braced export {} declaration.

Actual Result

/tmp/test/foo.d.ts
  2:9   error  'bar' is defined but never used   @typescript-eslint/no-unused-vars
  3:13  error  'Baz' is defined but never used   @typescript-eslint/no-unused-vars
  4:11  error  'quux' is defined but never used  @typescript-eslint/no-unused-vars

✖ 3 problems (3 errors, 0 warnings)

Additional Info

Similar to #2456, which was about a different kind of implicit export in .d.ts files, namely interface declarations.

Versions

package version
@typescript-eslint/eslint-plugin 4.9.1
@typescript-eslint/parser 4.9.1
TypeScript 4.1.2
ESLint 7.15.0
node 14.15.1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 20 (14 by maintainers)

Most upvoted comments

To anyone seeing this issue.

This issue is ONLY about @typescript-eslint/no-unused-vars reporting false-positives for exported namespaces in .d.ts files.

ALL OTHER ISSUES ARE NOT RELATED TO THIS ISSUE.

We have an FAQ you should read before commenting

I was hoping to add this rule to DefinitelyTyped (https://github.com/microsoft/DefinitelyTyped-tools/pull/968), but noticed this problem as well as most namespaces on DT have implicit export modifiers. I may look into this (though please don’t take this as cookie licking and please still attempt it even if I forget or fail).

A gotcha is that this namespace does not have any unused vars:

export namespace Foo {
    const value: 1234;

    export import Something = something.Something;
}

Yay namespaces.

@TheDutchCoder Your code has no exported namespaces, so your issue is not this issue. (You haven’t indicated what your issue is, since at least the current release of @typescript-eslint doesn’t give any errors for your code, but in any event you’ll need to ask about it somewhere else.)

Just upgraded our very large repo to

-    "@typescript-eslint/eslint-plugin": "3.9.1",
-    "@typescript-eslint/parser": "3.9.1",
+    "@typescript-eslint/eslint-plugin": "4.9.1",
+    "@typescript-eslint/parser": "4.9.1",
-    "typescript": "3.9.6",
+    "typescript": "4.1.3",

and seeing a lot of @typescript-eslint/no-unused-vars errors. They seem to be false positives that trigger either by optional arguments or optional chaining. I have a minimal case here for optional chaining:

foo

As you can see removing optional chaining fixes the issue.

Code:

function toURI(a: any): any {
  return a;
}

export const isSameContext = (
  oneContextUri?: string | null,
  anotherContextUri?: string | null,
): boolean => {
  if (!oneContextUri || !anotherContextUri) return false;

  const contextUri1 = toURI(oneContextUri);
  const contextUri2 = toURI(anotherContextUri);

  return contextUri1?.isSameIdentity(contextUri2);
};