Skip to content

[import/named] Named type exports are not found #154

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
kripod opened this issue Jan 28, 2019 · 15 comments
Closed

[import/named] Named type exports are not found #154

kripod opened this issue Jan 28, 2019 · 15 comments
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@kripod
Copy link
Contributor

kripod commented Jan 28, 2019

Repro

{
  "rules": {
    // https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/named.md
    // "Note that type imports, as used by Flow, are always ignored."
    "import/named": "error"
  }
}

utils.ts:

export type ResponsiveValue<T = string | number | undefined> =
  | T
  | { [key: number]: T };

export type ResponsiveObject<T> = {
  [K in keyof T]: ResponsiveValue<T[K]>
};

FlexLayout.ts:

import * as CSS from 'csstype';
import { ResponsiveObject } from './utils';

export type Props = ResponsiveObject<{
  flexDirection?: CSS.FlexDirectionProperty;
  flexWrap?: CSS.FlexWrapProperty;
}>;

Expected Result

No warnings should be emitted by ESLint.

Actual Result

[eslint] ResponsiveObject not found in './utils' [import/named] on line 2 of FlexLayout.ts

Versions

package version
@typescript-eslint/eslint-plugin 1.1.0
@typescript-eslint/parser 1.1.0
TypeScript 3.2.4
ESLint 5.12.1
node 10.15.0
npm 6.4.1
@kripod kripod added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jan 28, 2019
@armano2
Copy link
Collaborator

armano2 commented Jan 28, 2019

You should apply parser option https://github.com/benmosher/eslint-plugin-import#importparsers

if this is not going to work, please report issue at eslint-plugin-import

@armano2 armano2 closed this as completed Jan 28, 2019
@armano2 armano2 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 Jan 28, 2019
@armano2 armano2 reopened this Jan 28, 2019
@kripod
Copy link
Contributor Author

kripod commented Jan 28, 2019

Thank you for the quick response!

I'm using the following configuration along with eslint-import-resolver-typescript@1.1.1 and it doesn't work:

"settings": {
  "import/parsers": {
    "@typescript-eslint/parser": [".ts", ".tsx"]
  },
  "import/resolver": {
    "typescript": {}
  }
},

@kripod
Copy link
Contributor Author

kripod commented Jan 28, 2019

Actually, things seem to work fine when I remove the following:

"import/parsers": {
  "@typescript-eslint/parser": [".ts", ".tsx"]
},

Could the removal affect the quality of code analysis when using eslint-import-resolver-typescript?

@bradzacher
Copy link
Member

You shouldn't need this config.
I believe eslint-plugin-import should default to the configured ESLint parser.

If a file extension is matched, the dependency parser will require and use the map key as the parser instead of the configured ESLint parser

@kripod
Copy link
Contributor Author

kripod commented Jan 30, 2019

@bradzacher Sounds great, thank you!

@amwill04
Copy link

amwill04 commented Mar 5, 2019

@kripod, @bradzacher removing

"import/resolver": {
    "typescript": {}
  }

will break imports that are defined in tsconfig#paths option. Giving the error import/no-unresolved. If you dont use that then it "fixes" the issue. But it is still a bug as I do.

@bradzacher
Copy link
Member

I believe the intended config is:

{
  "settings": {
    "import/resolver": {
      "typescript": {}
    }
  }
}

You can remove the separate config item "import/parsers", as it will default to eslint's default parser.

slowbackspace added a commit to trezor/trezor-ui-components that referenced this issue May 9, 2019
@davetapley
Copy link

@amwill04 did you find a workaround for this which also allows for a non-empty:

  "import/resolver": {
      "typescript": {}

E.g:

    "import/resolver": {
      "typescript": "./tsconfig.json"
    }

@bradzacher @kripod I would like this issue re-opened, because:
Whilst the 'intended config' given above (i.e. "typescript": {}) does work around the issue, it also prevents use of a custom tsconfig.json.

I propose that a 'real fix' would resolve the [import/named] Named type exports are not found issue regardless of what's in "import/resolver": {.


As further proof this is a bug, observe that there is no import/named error when importing:

export interface IJob {
  foo: string;
}

But importing this would cause an import/named error:

export type IJob = {
  foo: string;
}

@bradzacher
Copy link
Member

bradzacher commented Jul 31, 2019

A few things:

1

I won't be reopening this, because it clearly isn't a bug with this project.
It's an issue with a eslint-plugin-import rule, which is failing when eslint-import-resolver-typescript is configured.

2.

How come you're even using import/named?
Typescript will handle this for you automatically.

3.

Reading the documentation for the import resolver: https://github.com/alexgorbatchev/eslint-import-resolver-typescript, it looks like you've misconfigured the resolver.
The correct config is actually:

      // use <root>/path/to/folder/tsconfig.json
      "typescript": {
        "directory": "./path/to/folder"
      }

https://github.com/alexgorbatchev/eslint-import-resolver-typescript/blob/9c37a6cf1fbc2c563ca79d61523ef0affd8badd2/index.js#L34-L36

@davetapley
Copy link

Thanks for the fast reply @bradzacher.

Good catch with 3, fixed that.

For 2 do you mean:
"why are we using import/named when TypeScript itself would fail to compile if an import is missing"?
If so then I agree! Is that something we should be disabling ourselves with overrides in .eslintrc, or should the extends listed here take care of that?

For 1 I'm happy to open an issue on https://github.com/benmosher/eslint-plugin-import, but can I just clarify what the issue is:
Are you thinking that it should be the responsibility of eslint-import-resolver-typescript to not do import/named if it sees eslint-import-resolver-typescript is configured?

@bradzacher
Copy link
Member

Are you thinking that it should be the responsibility of eslint-import-resolver-typescript to not do import/named if it sees eslint-import-resolver-typescript is configured?

This is an inherent problem with eslint as it stands right now. In order to configure the import resolver, you have to use the "settings" field on your eslintrc. Unfortunately however - unlike with rule config, which is validated against a JSON schema, eslint provides no mechanism for defining the structure of the settings object.
Similarly, eslint-plugin-import does not provide a mechanism for defining the structure of the resolver config.

So unfortunately the whole system relies upon the users reading the documentation.

if you were to raise an issue, it would be with import-resolver-typescript - as it should probably have better validation of the configuration it's expecting.

Is that something we should be disabling ourselves with overrides in .eslintrc, or should the extends listed here take care of that?

We do not enable the rule from our plugin configs. (and we cannot, because doing so would require we have a dependency on the plugin).

Taking a guess, I think you're using the recommended config from eslint-plugin-import?
If so , then it's the former - you should disable the rule in your eslintrc.

@davetapley
Copy link

Thanks for taking the time to write such a comprehensive reply @bradzacher 😄
We've got it all working now, we disabled the rule as you suggested.

@cn3lfs
Copy link

cn3lfs commented Sep 17, 2019

use eslint-import-resolver-ts. https://github.com/rx-ts/eslint-import-resolver-ts#configuration

@joaovieira
Copy link

@dukedave @bradzacher following on that discussion, shouldn't disabling those useless rules go into https://github.com/benmosher/eslint-plugin-import/blob/master/config/typescript.js ?

@bradzacher
Copy link
Member

yup.
but not everyone thinks they're useless - not everyone uses the typescript compiler to typecheck their code (i.e. some people use babel + webpack), so they rely on eslint tooling to check things like imports.

This is why the import resolver exists - so people can use these tools if they want.
I would recommend just disabling it locally if you don't find value in it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

7 participants