-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Typescript cannot locate the type information of the core module. #2682
Comments
Is there any easy way we can tell it that that is essentially the same as importing the whole library at least from a types perspective?
This isn't going to happen. |
Well, really quick solution would be to move/declare your types to
core.d.td
index.d.ts
Of course with this fix |
Why include this at all? No changes need to be made here, correct or no? Did you include it just for "context"? |
You've already declared |
And I assume my VS Code "intelligence" for all things type related (even though our project is annotated JS) will continue to just work since I think it's all predicated upon this magic line in package.json, right? If so I think a PR for this would be more than welcome. |
Sure, I will. |
you cannot import anything that is in a sub-folder because typescript is not able to locate the type definitions for now, the only solution that worked for me is a reference comment like this: /// <reference path="../../node_modules/highlight.js/types/index.d.ts" /> |
I'm just waiting for a simple PR to solve this. @hallaji Made it sound pretty trivial... If someone could provide a small repository test project with steps to reproduce the issue it'd be much easier to test the fix when someone makes a PR. @thealjey IE, if you're saying the proposed fix won't work then having a simple test case would go a long ways toward helping us find a proper fix. I'm quite confused why simply moving the file would help. It seems the WHOLE purpose of the My understanding of this issue:
|
Why? Isn't this the whole purpose of
I don't find any improvement after moving the file. |
I don't understand why this seems to work for faker, but not us: As far as I can tell we're following the exact same pattern with |
Did you try I'm not sure why tsk needs a stub package to work properly though. |
This does not fix the issue for me. |
Maybe that's not needed anymore? I really don't understand all the "magic" that is happening here as far as when and where TS looks for type files. |
I believe you, but the version NPM pulled for me today doesn't: # npm install @types/highlight.js
up to date, audited 165 packages in 840ms
17 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
# ls node_modules/@types/highlight.js
LICENSE README.md package.json It may not be what you're looking for as a test case, but I have a pretty simple real-world use case here: https://gitlab.com/ethan.reesor/vscode-notebooks/simple-renderers/-/blob/master/src/highlight/main.ts The |
DO you need to be inside a TS project or something? Not sure but I saw this yesterday but today in my test project it works fine:
|
|
|
OK wow... - @types/highlight.js
+ @types/highlightjs As far as I'm concerned, that solves my problem. |
Oh yikes and that's not even the correct name either - I didn't realize @types had two. ugh. Lets see if I can fix this now. :) |
@hallaji Please try:
Right now you need |
And now I'm even more confused how TS finds the types though... does it just load EVERYTHING with |
It must, unless it does something like compare after removing all non-alphanumeric characters. I wonder if Typescript's normal type resolution ( Either way, this issue is pretty frustrating (from your perspective) - the import highlightjs = require('highlight.js');
export = highlightjs; If that works, why don't the definitions in your package. Maybe this is a |
Indeed. But seems pretty silly since it's easy to see that the longer strings points into a package and therefore it could use the package type modules to potentially help resolve the reference. Not sure why that would be a bad thing to try. |
I'm still running this down (to see what we might want to change but seems you can also fix this simply by first importing the top-level package) though I'm not sure what packaging implications that might have (if any): import 'highlight.js' // this line makes the following line know where the types are
import hljs from 'highlight.js/lib/core' This is really all the |
@andrewbranch If we just go ahead and add We have a lot of imports that are effectively the same:
All of these export the same types... just the amount of grammars they add vary greatly. I suppose technically our build system could just duplicate the type file multiple times, but I'm sure that's technically not the correct way to do these things so I'd love to know the right way. |
Yeah, I think importing from index.d.ts would make sense in this case. |
And index.d.ts can still live in the |
@andrewbranch Would you mind having a look at #3073 Nothing seems to work, or it only half works... I have:
IN a test project but I keep getting:
I don't understand WHERE it wants the ambient export or what triggers the ambient exports to "count", ie if core just imports index, does that magically bring all the ambient exports into scope... Right now I have the ambient export pattern in BOTH index and core, and still it doesn't work. |
OK the only way I've gotten it to work so far is:
I'll push what I have... is this the right track? |
@firelizzard18 @hallaji Any chance either of you could test the changes in the linked PR? |
@joshgoebel It works for |
In my sample project that works. The import path must literally be:
|
@joshgoebel When I ran |
Sounds about right. TS is picky. :) |
Closing via #3073. |
sigh And upon moving all the types into Is there some way to fix this? |
FYI setting reference seems to play nice as near as I can tell. In my case the file importing core is in src/filename.ts /// <reference path="../node_modules/highlight.js/types/index.d.ts" />
import hljs from 'highlight.js/lib/core'; From there register your languages: import typescript from 'highlight.js/lib/languages/typescript';
hljs.registerLanguage('typescript', typescript); |
Hi there 👋
I'd like to import the core and only necessary language files. I tried as below but then realised that there is something wrong in package regarding typings.
I have not imported the index module i.e.
import hljs from 'highlight.js
. So, typescript can't locate the type information for the core module from a subfolder. I believe the solution might be creating.d.ts
file for every subfolder and file in your case.I quickly fixed this issue locally by creating a
core.d.ts
file underlib
directory. So, I guess we need to declare the entire API that the library exposes.The text was updated successfully, but these errors were encountered: