Eslint-plugin-import: TypeScript export and import/named rule

Created on 6 Feb 2019  路  22Comments  路  Source: benmosher/eslint-plugin-import

TypeScript's exported types/interfaces does not work with import/named rule.

File 'xxx.ts':

export type ScpRouteProps = ReactRouter.RouteProps & {
    access?: boolean;
    title: string;
};

Then import it in different file:

import {ScpRouteProps} from './xxx';

Will result in error:

7:5  error  ScpRouteProps not found in './xxx'    import/named

Same error when importing:

import {WrappedFieldProps} from 'redux-form';
// 1:9  error  WrappedFieldProps not found in 'redux-form'                import/named

Or

import {AutoSizer, Column, ColumnProps, Table, TableProps} from 'react-virtualized';
// /xxx/web/src/components/VirtualizedTable.tsx
//  3:28  error  ColumnProps not found in 'react-virtualized'  import/named
//  3:48  error  TableProps not found in 'react-virtualized'   import/named
help wanted typescript

Most helpful comment

it seems to only happen when the type are in a separate index.d.ts file

All 22 comments

It seems like the right thing to do here is to include TypeScript export type exports in the ExportMap, which should make everything Just Work.

Not just type, interfaces too. Basically, all "not real" exports. Any ideas how to do that? I'm totally not familiar with plugin's codebase...

Getting the same error

I'm getting the same error in my Gatsby.js TS project.

May also want to check a package's package.json for a types key which points to a TypeScript type definition file (*.d.ts).

Weirdly export type foo isn't recognized for me anymore, while export interface bar is.

Was the ExportMap a comment about how to fix this bug via PR or is there some config to fix this? Just hit this one as well :-\ pretty major rule to disable.

Ah ok guessing the case statement needs to just have whatever ts is calling its type declarations?

case 'TSEnumDeclaration':
case 'TSInterfaceDeclaration':
case 'TSAbstractClassDeclaration':
case 'TSModuleDeclaration':

@ljharb got this fixed in pr #1304 - any way to get this merged ASAP? It kills our libraries pretty badly and we are a large part through moving to eslint from tslint. :-)

@bradennapier even if it were; there's only one person who can cut a release, so it's not likely to be published asap.

Ahh, ok thanks.

FYI, this only appears to happen when using @typescript-eslint/parser, but works correctly with (the now-deprecated) typescript-eslint-parser.

like @schmod said the bug is present with when using @typescript-eslint/parser and unfortunately still present, I dont understand why, @bradennapier fix seemed ok :suspect:

it seems to only happen when the type are in a separate index.d.ts file

I'm getting this issue with @typescript-eslint/parser as well. Perfectly fine named exports are erroring while being imported.

import { ThemeProps } from 'styled-components' currently errors.

I'm experiencing this error as well, with the package react-intl v2.9.0.

import { InjectedIntlProps } from 'react-intl'; currently errors.

So in the alexgorbatchev/eslint-import-resolver-typescript#31 it passed to eslint-plugin-import and here pass it back? how I can help to resolve this issue?
Thank you

I'm still getting this, for example when importing the Keyframes interface from styled-components:

import { Keyframes as KeyframesType } from 'styled-components'

it seems to only happen when the type are in a separate index.d.ts file

The same. Is there any solution? We have to disable the rule for now.

Issue seems to appear when importing interface from @types folder. If package has index.d.ts in it's own folder - it works fine.

Adding this to .eslintrc.js:

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

seems to solve this problem.
Unfortunetly, it introduces another issue: No default export found in imported module "react" import/default when doing import React from "react" or import * as React from "react".

That鈥檚 because typescript鈥檚 module system is broken unless you enable synthetic imports and es module interop. If you enable those, what happens?

Just to give a feedback, i had same issue and had all tried that should fix this issue. Nothing helped. So i played more with the settings and fixed it with setting the babel/env preset modules to "commonjs". Was set to "false".
So babelrc:

"presets": [
        [
            "@babel/env",
            {
                "useBuiltIns": "entry",
                "corejs": 3,
                "modules": "commonjs"
            }
        ],
        "@babel/react",
        "@babel/typescript"
    ]
Was this page helpful?
0 / 5 - 0 ratings