Skip to content

[no-redeclare] error for declare namespace #60

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
ficristo opened this issue Dec 24, 2018 · 7 comments
Closed

[no-redeclare] error for declare namespace #60

ficristo opened this issue Dec 24, 2018 · 7 comments
Labels
bug Something isn't working package: parser Issues related to @typescript-eslint/parser scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser

Comments

@ficristo
Copy link

Repro
See below.

{
  "rules": {
    "no-redeclare": "error"
  }
}
export = CodeMirror;
export as namespace CodeMirror;

declare function CodeMirror(host: HTMLElement, options?: CodeMirror.EditorConfiguration): CodeMirror.Editor;

declare namespace CodeMirror {
}

Expected Result
No error.

Actual Result
no-redeclare error for declare namespace CodeMirror line.

Additional Info

Versions

package version
eslint-plugin-typescript 1.0.0-rc.0
typescript-eslint-parser eslint-plugin-typescript/parser
typescript 3.1.6
@bradzacher
Copy link
Member

bradzacher commented Jan 8, 2019

Investigating this case, it's hard for us to deal with this because it's valid typescript.

TS augments the the base named thing with the namespace, so in this case, it is completely valid code.
The question is; should this should be caught by our linter if it's completely valid code?

We would have to bring in and redefine the no-redeclare rule to make this work.

Concrete code:

function CodeMirror(arg: string): boolean {
    return true;
}

namespace CodeMirror {
    export const foo = 1
}

CodeMirror.foo // type === number
CodeMirror("hi") // valid

Declarations only:

declare function CodeMirror(arg : string): boolean;

declare namespace CodeMirror {
    export const foo = 1
}

CodeMirror.foo // type === number
CodeMirror("hi") // valid

repl

@armano2
Copy link
Collaborator

armano2 commented Jan 8, 2019

@bradzacher right now parser is not providing consistent informations about declarations, than we can't really do much for now here.

@JamesHenry JamesHenry transferred this issue from bradzacher/eslint-plugin-typescript Jan 18, 2019
@bradzacher bradzacher added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin package: parser Issues related to @typescript-eslint/parser and removed package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin labels Jan 18, 2019
@bradzacher bradzacher added the scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser label Apr 15, 2019
@despian
Copy link

despian commented Jun 20, 2019

+1 have also come across this issue.

Would be nice if declaration-merging was supported.

@G-Rath
Copy link
Contributor

G-Rath commented Jan 8, 2020

This also errors on global augmentations, which seems fixable to me b/c it actually only happens when wrapped in a declare global:

declare global {
  namespace jest {
    interface Matchers<R, T> {
      /**
       * Tests that the expected {@link TFBlockBody} contains only one {@link TFDynamicBlock}
       * with the given `name`
       *
       * @param {string} name
       */
      toContainTFDynamicBlock(name: string): R;
    }
  }
}

So you should just be able to look for the declare global and go off that.

Let me know if you'd like a separate issue to track this, or if I've missed something :)

@G-Rath
Copy link
Contributor

G-Rath commented Jan 8, 2020

Additionally, a question: is no-redeclare actually picking up errors that TypeScript itself isn't?

Aside from the potentially nicer error messages, I'm wondering how much value there is in actually having no-redeclare in a TypeScript codebase?

Just noticed it is actually turned off in the eslint-recommended config provided by @typescript-eslint/eslint-plugin, so that answers that question.

@bradzacher
Copy link
Member

In response to your second comment - AFAIK it doesn't catch anything.
For these sorts of rules which catch things typescript catches itself, usually people use it because either:

  • they have webpack builds wherein they use the typescript in isolated modules mode (or they use babel to transpile).
  • they haven't thought about the fact that it does something TS does.

This duplicating a TS compiler error is why this issue (and really all scope analysis rules) have been sitting open for 1yr+++. To the core maintainers the issues are high effort + low value, and there's yet to be a contributor that has cared enough to look into it.

@bradzacher
Copy link
Member

Merging into #1856

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: parser Issues related to @typescript-eslint/parser scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser
Projects
None yet
Development

No branches or pull requests

5 participants