Skip to content
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

[no-undef] Exported interface is marked as unused #131

Closed
timdp opened this issue Jan 23, 2019 · 17 comments
Closed

[no-undef] Exported interface is marked as unused #131

timdp opened this issue Jan 23, 2019 · 17 comments
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin package: parser Issues related to @typescript-eslint/parser scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser

Comments

@timdp
Copy link

timdp commented Jan 23, 2019

What code were you trying to parse?

https://github.com/timdp/standard-typescript-interfaces

What did you expect to happen?

I'm trying to use Standard with TypeScript.

The compiler allows me to export an interface from types.ts and import it into index.ts, but the linter and formatter complain.

My goal is actually to create a shared types package in a monorepo, but I've simplified it to a plain old module.

I'm well aware that interfaces don't exist in JavaScript, but given that tsc allows me to do this, ESLint should understand it as well, right?

What actually happened?

$ standard '*.ts'
standard: Use JavaScript Standard Style (https://standardjs.com)
  .../index.ts:1:10: 'Foo' is defined but never used.
  .../types.ts:11:10: 'Foo' is not defined.

Versions

package version
@typescript-eslint/parser 1.1.0
TypeScript 3.2.4
ESLint 5.4.0
node 10.15.0
npm yarn 1.13.0
@timdp timdp added package: parser Issues related to @typescript-eslint/parser triage Waiting for maintainers to take a look labels Jan 23, 2019
@j-f1
Copy link
Contributor

j-f1 commented Jan 23, 2019

Make sure you’ve enabled the @typescript-eslint/no-unused-vars rule from the @typescript-eslint plugin.

@bradzacher bradzacher added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin and removed package: parser Issues related to @typescript-eslint/parser labels Jan 23, 2019
@timdp
Copy link
Author

timdp commented Jan 23, 2019

That would avoid the unused-import warning but the type is actually used. Note the second error.

@j-f1
Copy link
Contributor

j-f1 commented Jan 24, 2019

Does installing the latest @typescript-eslint/eslint-plugin help?

@timdp
Copy link
Author

timdp commented Jan 24, 2019

I just updated the code to do that. Same error though.

@armano2 armano2 added bug Something isn't working and removed triage Waiting for maintainers to take a look labels Feb 3, 2019
@bradzacher
Copy link
Member

Could you please chuck a succinct repl case into the OP?

It's a bit hard for us to reproduce and fix the issue if we have to checkout your entire repo to investigate.

@armano2
Copy link
Member

armano2 commented Feb 4, 2019

@bradzacher
Copy link
Member

bradzacher commented Feb 4, 2019

I'm assume standard just uses the recommended config?
This is what I mean by creating a succinct repl - the level of indirection with a tool makes it a bit harder to assess. Have to switch between a repo and its files.. not having rule names in the output..

@bradzacher
Copy link
Member

bradzacher commented Feb 4, 2019

Reviewing the source for no-undef, it looks to me like a bug with the scope extensions that we're doing - the interface is not added to the global variable scope.
https://github.com/eslint/eslint/blob/master/lib/rules/no-undef.js#L59-L73

@bradzacher bradzacher added the package: parser Issues related to @typescript-eslint/parser label Feb 4, 2019
@timdp
Copy link
Author

timdp commented Feb 4, 2019

Cool, cool. Still need the REPL demo then?

@armano2
Copy link
Member

armano2 commented Feb 4, 2019

@bradzacher to be precise, non of "type" nodes are not added to scope,
type Foo, interface, typeParameters, typeArguments and so on.

@mubaidr
Copy link

mubaidr commented Apr 13, 2019

Any update?

@bradzacher
Copy link
Member

I would suggest not using no-undef on typescript code.
The typescript compiler should catch all of the cases itself, so you shouldn't need the rule.

We'll look into a fix, though it will not be soon.
Scope analysis is a difficult thing when you add in the multiple and shared scopes that typescript adds.

@bradzacher bradzacher changed the title Cannot export interface? [no-undef] Exported interface is marked as unused Apr 15, 2019
@mubaidr
Copy link

mubaidr commented Apr 16, 2019

^ Yes, makes sense. 👍

@bradzacher bradzacher added the scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser label Apr 19, 2019
@meikidd
Copy link

meikidd commented Jun 21, 2019

Same issue here. Below is my code, it is allowed by typescript, but the linter deny it.

type User = {
  id: number;
  name: string;
};

export default User;

linter error:

./src/models/user.ts
  Line 6:  'User' is not defined  no-undef

wmfgerrit pushed a commit to wikimedia/wikibase-termbox that referenced this issue Jun 24, 2019
During the termbox hike and now in the data-bridge hike was mentioned,
that it would benefical to get the eslint settings from a dedicated
place.
To make this happen this patch makes some initial aligments to the
existing config in termbox and will use the same config in data-bridge
as well. This will be a also the base for a upcomming pull request to https://github.com/wikimedia/eslint-config-wikimedia.
It also algines with
https://eslint.org/docs/user-guide/configuring#configuration-file-formats
in terms of not using deprecated formats anymore.
See also: I2df9b5e31d90546db458e9c8dfabb0d3601b2434
and: I22115b36ea4864ff98761c5ba9e842316b1e5329

Note: This pathc contains a workaround for the "no-undef" issue. For tracking or futher reading see: typescript-eslint/typescript-eslint#131 and eslint/typescript-eslint-parser#437

Bug: T225952
Change-Id: I6c635edb9c75cc79031bcda5d0c7619ba2218a4d
@mockdeep
Copy link

We're running into this same issue. And, unfortunately, TypeScript does not catch the undefined globals because in this case we're dealing with JQuery. In the type defs for JQuery, it is declared as a global, so TypeScript ignores references to it. For various reasons, we're trying to move to locally importing JQuery in our files, so the no-undef rule is pretty valuable to making sure we don't miss anything.

The workaround for the moment, was to move the type it was complaining about to being a global type def in a .d.ts file.

@juanmait

This comment has been minimized.

@bradzacher bradzacher added this to the scope analysis rewrite milestone Apr 6, 2020
@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: eslint-plugin Issues related to @typescript-eslint/eslint-plugin 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

8 participants