8000 [no-unused-vars] False positives with members of exported namespace in .d.ts file · Issue #2867 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

[no-unused-vars] False positives with members of exported namespace in .d.ts file #2867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
andersk opened this issue Dec 11, 2020 · 20 comments · Fixed by #10714
Closed
3 tasks done

[no-unused-vars] False positives with members of exported namespace in .d.ts file #2867

andersk opened this issue Dec 11, 2020 · 20 comments · Fixed by #10714
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@andersk
Copy link
Contributor
andersk commented Dec 11, 2020
  • 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
@andersk andersk added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Dec 11, 2020
@bradzacher
Copy link
Member

Why are you exporting your namespace here?

Also in declaration files - you should be declareing your namespaces.

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels Dec 11, 2020
@andersk
Copy link
Contributor Author
andersk commented Dec 11, 2020

@bradzacher In a .d.ts file, export declare is the same as just export. It declares a namespace that’s an exported member of the module, to be used like import {Foo} from "./foo"; Foo.Baz.quux.

Just declare namespace without export would be different; that would declare a namespace that’s a global variable, to be used like import "./foo"; Foo.Baz.quux

@bradzacher
Copy link
Member

How are you creating your declaration files for your modules?

I thought the general pattern was

declare module './foo' {
  export namespace Foo {
    // ..
  }
}

and not just declaring your namespaces in the root.

@andersk
Copy link
Contributor Author
andersk commented Dec 11, 2020

With a text editor?

A foo.d.ts file can either (a) declare types for third-party modules with declare module 'lodash', or (b) declare types for foo.js in the same directory at the root. For a module in the same directory, declare module './foo' is actually illegal (TS2436: Ambient module declaration cannot specify relative module name); you can only do (b).

8000
@bradzacher bradzacher added bug Something isn't working and removed awaiting response Issues waiting for a reply from the OP or another party labels Dec 11, 2020
@bradzacher
Copy link
Member

The issue is that our selectors target Program > Namespace, so because your namespace is exported it doesn't match this.

[ambientDeclarationSelector(AST_NODE_TYPES.Program, true)](

Happy to accept a PR - simple fix.

@danielrsantana

This comment has been minimized.

@bradzacher

This comment has been minimized.

@dopecodez
Copy link
Contributor

I'll take a look at this 👨🏽‍💻

@tryggvigy

This comment has been minimized.

@bradzacher

This comment has been minimized.

@bradzacher

This comment has been minimized.

@danielrsantana

This comment has been minimized.

@danielrsantana

This comment has been minimized.

@bradzacher

This comment has been minimized.

@typescript-eslint typescript-eslint locked as off-topic and limited conversation to collaborators Jan 2, 2021
@bradzacher bradzacher added the has pr there is a PR raised to close this label Jan 2, 2021
@bradzacher
Copy link
Member

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

@typescript-eslint typescript-eslint unlocked this conversation Jan 2, 2021
@JounQin

This comment has been minimized.

@bradzacher

This comment has been minimized.

@TheDutchCoder

This comment has been minimized.

@andersk

This comment was marked as off-topic.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@JoshuaKGoldberg JoshuaKGoldberg removed the has pr there is a PR raised to close this label Aug 5, 2023
@jakebailey
Copy link
Collaborator

I was hoping to add this rule to DefinitelyTyped (microsoft/DefinitelyTyped-tools#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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
9 participants
0