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

Handle imports from @types/foo type-only packages #113

Open
bradzacher opened this issue Mar 14, 2024 · 8 comments
Open

Handle imports from @types/foo type-only packages #113

bradzacher opened this issue Mar 14, 2024 · 8 comments

Comments

@bradzacher
Copy link

In typescript it's possible to import from a module that only has a @types/ definition.
Example -- if you "right click -> peek definition" on the import source you'll see it mentions node_modules/@types/google__maps.

From what I can tell there isn't a way to resolve this import without manually specifying a mapping.
Am I correct in this? Or is there a way to support it?

@Boshen
Copy link
Member

Boshen commented Mar 14, 2024

It seems like we'll need to add a new @types lookup feature, to align with TypeScript's @types resolution.


I don't know anything about @types lookup, I can't even figure out how it goes from @google/maps to https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google__maps and not https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google-maps 😅

@bradzacher
Copy link
Author

https://github.com/microsoft/TypeScript/blob/952dfc508256fea697aacc431edb81e3f23033b5/src/compiler/moduleNameResolver.ts#L3133-L3147

It looks like hard-coded behaviour.
TL;DR:

`@types/${packageName.startsWith('@') ? packageName.slice(1).replace('/', '__') : packageName}`

@Boshen
Copy link
Member

Boshen commented Mar 14, 2024

I think I can get this implemented if it's trivial:

  • add an option to enable @types lookup
  • do the naming conversion
  • do the resolution
  • return an extra field for @types resolution

@Boshen
Copy link
Member

Boshen commented Mar 29, 2024

Is it possible to create another resolver instance with the modules option pointing to the @types directory? I think @Dunqing figure it out somehow.

Here's a similar example to resolve tsconfig.json:

oxc-resolver/src/lib.rs

Lines 1061 to 1066 in 4226f7a

.clone_with_options(ResolveOptions {
description_files: vec![],
extensions: vec![".json".into()],
main_files: vec!["tsconfig.json".into()],
..ResolveOptions::default()
})

@Dunqing
Copy link
Member

Dunqing commented Mar 29, 2024

Is it possible to create another resolver instance with the modules option pointing to the @types directory? I think @Dunqing figure it out somehow.

I want to confirm the scope of this instance. Just resolve @types/* packages? Or for all packages, resolve out the corresponding type declarations.

Here are the different types of packages that I have listed

// no type declarations but has @types
import { LatLng } from '@google/maps'; // resolved path: file:///node_modules/@types/google__maps/index

// type only package
import {} from 'undici-types' // resolved path: file:///node_modules/undici-types/index

// both js source files and corresponding type declarations
import { } from 'vite' // resolved path: file:///node_modules/vite/dist/node/index

// js only package
import {} from 'js-only-pacakage' // resolved nothing

If that's what we're going to do, I think we can do a lot of linter rules about .d.ts

@Boshen
Copy link
Member

Boshen commented Mar 29, 2024

The resolution will return both the package path as well its types packages.

I was thinking about implementing this in user land, but seems a bit difficult after a second thought.

@Dunqing
Copy link
Member

Dunqing commented Mar 29, 2024

If my understanding is correct, I think we should do this

  • add an option to enable type resolution
  • try to look at types and typings fields in package.json if not do the @types lookup
  • do the resolution
  • return an extra field for type resolution

@Sec-ant
Copy link

Sec-ant commented Apr 3, 2024

Just being curious, are ambient modules also in the scope of this issue/package?

image

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

No branches or pull requests

4 participants