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

Importing modules from d.ts #72

Closed
jeffrson opened this issue Apr 29, 2021 · 9 comments
Closed

Importing modules from d.ts #72

jeffrson opened this issue Apr 29, 2021 · 9 comments

Comments

@jeffrson
Copy link

Hi,

I have a case within a project that I tried to reduce to a minimal repro, see here: https://github.com/jeffrson/import_unresolved_dts

eslint with eslint-import-resolver-typescript apparently fails to recognize a module from d.ts, whether or not it is located in / or /@types.

startup.ts looks like this

import { ITest } from 'module1'

const i: ITest = { test1: '1' }
console.log(i)

and there's a file called my_modules.d.ts (name shouldn't matter):

declare module 'module1' {
    export interface ITest {
        test1: string
    }
}

which, when linting, shows the import/no-unresolved error.

@JounQin
Copy link
Collaborator

JounQin commented Apr 29, 2021

No idea how to fix this case easily. Maybe @bradzacher can help, because @typescript-eslint/eslint-plugin should also need to read custom .d.ts definitions.

But in another thought, eslint-plugin-import will just continue to resolve modules from node_modules, it is clear that this is no module1 source file (.js)

And also, if there are multiple modules defined at a single .d.ts, it will trigger a import/no-duplicates reporting: import-js/eslint-plugin-import#1479

So there will be no fix quickly.

@bradzacher
Copy link
Collaborator

Typescript-ESLint is built directly on top of TypeScript, so we don't need to worry about this. TS parses and checks all files together, so it implicitly finds and applies the module declaration.

This is essentially impossible for the resolver to handle. A .d.ts file can be literally anywhere in the project tree. There's nowhere special you can do to look for it.

If the module declaration declares a module from node_modules then it's fine and will "just work". But if you're defining fake modules that don't exist on disk...

Maybe consider instead defining a file somewhere in your project and using paths to map a name to it?

@JounQin
Copy link
Collaborator

JounQin commented Apr 29, 2021

@bradzacher Do you know is there a TypeScript API to search that?

@bradzacher
Copy link
Collaborator

There's probably something attached to the ts.Program object that comes back from the parse (which is returned as part of our parserServices.

Though I don't know offhand. There's a lot of data exposed on its properties and via the type checker.

TBH though if this project starts touching the type-information you'd really just be rebuilding typescript's module checks using typescript. And at that point you might as well just scrap all the logic and instead just gather and report the TS diagnostics.

@JounQin
Copy link
Collaborator

JounQin commented Apr 29, 2021

Yeah, I don't know whether worth to do that. I will do a research first recently.

@JounQin
Copy link
Collaborator

JounQin commented Jun 24, 2021

I think you should just use import/ignore or an ESLint disable comment for this case now. It's not worth to include TypeScript into this resolver because TypeScript itself has already done it.

@JounQin JounQin closed this as completed Jun 24, 2021
@JounQin
Copy link
Collaborator

JounQin commented Jun 24, 2021

Or we can provide a simple mapper option to declare the mapping manually, if you want, PR welcome.

@linonetwo
Copy link

Same here
截屏2023-06-23 00 43 28
截屏2023-06-23 00 43 35

@linonetwo
Copy link

#197

#224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants