Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Import declaration conflict not detected with import { Foo } from statement #2921

Closed
SamVerschueren opened this issue Jun 13, 2017 · 6 comments · Fixed by #2598
Closed

Import declaration conflict not detected with import { Foo } from statement #2921

SamVerschueren opened this issue Jun 13, 2017 · 6 comments · Fixed by #2598

Comments

@SamVerschueren
Copy link

Bug Report

  • TSLint version: 5.4.3
  • TypeScript version: 2.3.4
  • Running TSLint via: (pick one) CLI

TypeScript code being linted

foo.ts
interface Foo {
    [key: string]: any;
}
main.ts
import { Foo } from './foo';

const Foo = 'bar';

with tslint.json configuration:

{
	"rules": {
		"no-shadowed-variable": true
	}
}

Actual behavior

TSLint doesn't catch that Foo is being redefined.

Expected behavior

TSLint should fail with the following error

[tslint] Import declaration conflicts with local declaration of 'Foo'

At first I thought this had something to do with the no-shadowed-variable rule, but now I don't think it is the case anymore. Whenever you change main.ts to something like this

import * as Foo from './foo';

const Foo = 'foo';

It detects the conflict and throws an error.

@ajafff
Copy link
Contributor

ajafff commented Jun 13, 2017

I added this to my PR #2598

Though you won't get a lint error for your code snippet above, because both declarations are in the same scope. But you get a compile error for that anyway.

But the following will work as expected:

import { Foo } from './foo';
{
    const Foo = 'bar'; // error: shadowed name 'Foo'
}

@SamVerschueren
Copy link
Author

Shouldn't my code snippet produce the following error then?

[tslint] Import declaration conflicts with local declaration of 'Foo'

If the following snippet produces that error, my initial one should do that as well as it's incorrect.

import * as Foo from './foo';

const Foo = 'foo';

So it's pretty nice that you covered that case, I think it only solves a part of the issue.

Thanks for looking into it btw :)!

@ajafff
Copy link
Contributor

ajafff commented Jun 13, 2017

Shouldn't my code snippet produce the following error then?

[tslint] Import declaration conflicts with local declaration of 'Foo'

It's actually [ts] Import declaration conflicts with local declaration of 'Foo' and does not come from tslint but the typescript compiler.

no-shadowed-variable only checks for shadowed variables, that means variables in different scopes. That does not include invalid declaration merging, because it's already handled by the compiler.
If you really want this error coming from tslint, you can use tslint -p . --type-check which will also check for compile errors.

@SamVerschueren
Copy link
Author

So I should rather create an issue in the TypeScript repository concerning the issue that it's not throwing the [ts] Import declaration conflicts with local declaration of 'Foo' error in case of the following snippet? Because it has nothing to do with TSLint?

import { Foo } from './foo';

const Foo = 'bar';

@ajafff
Copy link
Contributor

ajafff commented Jun 14, 2017

Yes, you should create an issue in the TypeScript repo if you don't see the expected error message.
Though I cannot reproduce that behavior locally. I get the correct error message for both named imports and namespace imports.

@SamVerschueren
Copy link
Author

Strange. I used the latest TypeScript but it doesn't give me the error. Took a screen capture. But will open this one in the TypeScript repository and link to this thread. Thanks for the information @ajafff!

tslint

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants