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 build problem: node_modules/highlight.js/types/index.d.ts is not a module #2603
Comments
Not sure what you did exactly to get that error? |
May need some TypeScript people to weigh in here... we only have the It's possible we may need to change something slightly? I'm not sure why TS would be trying to import it by default. |
Is this a regression since 10.0? I don't think this has changed from 10.0 to 10.1 |
Have tried v10.0.x and it built successfully. There is no types/index.d.ts under v10.0.x |
Ah, so it was more recent... but I still don't know why your TS is trying to load that file? I don't think I understand how typescript loads libraries with |
Maybe we need |
Suggest to use index.d.ts in @types/highlight.js. Or you can remove |
Only if it's in the root is my understanding from reading the docs. |
ref from https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types |
Same problems here. declare namespace hljs {
...
}
export = hljs;
export as namespace hljs; https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/highlight.js/index.d.ts |
If we just remove the types key from package.json will that solve this temporarily? This obviously needs more thought. |
Yes, it will fallback to @types/highlight.js. |
I'm trying to understand the bigger picture here. It seems it would be much better if we bundled this type file (so we can keep it maintained) rather than @types/highlight.js having a very old version. But does this magic only work with npm? I created a new TS project and put the build of HLJS in import hljs from './highlight.js'; This works fine (once I added the proper import basic from './highlight.js/lib/languages/basic.js'; I get:
This is despite having in the HLJS type file:
Am I missing something obvious? Is this impossible without npm? Surely not. |
You can check the last part of the document: https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html |
Reading. I think my question is more about "Ambient Modules" though. Obviously adding 185 https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules |
IE, How does this work:
Can it not be made to work with relative imports? |
And if I futz around I can get this error, but googling didn't help much:
|
Ok I gave up on the relative install and now I just installed it via npm (it's in package.json) but then just changed the
|
So now I'm wondering if |
Ok I think for some reason npm was pointed to GitHub... but now that I have the literal NPM highlight.js package (the only thing I'm updating it copying in the
I don't understand this last error at all... I thought the whole point of augmentation like this was to add types to untyped JS files... |
I'm not sure if I get this problem right, so I try to answer some things mentioned in this issue.
If you want to switch to a self-contained version that is installed with the normal js package, that's entirely possible (and even the preferred way of shipping types). But I'm not sure what things should be possible with the new types or what use cases are not covered by the types in the @types/* package. Could you elaborate on that? If it is planned for the future and it is open for contributions, I'd like to help with migrating hljs to TS. |
Yes, I understand the purpose of @types.
I do, that's what I'm trying to do here - unsuccessfully. Before removing it from @types first I need to get it working with HLJS. My index.d.ts doesn't work, and copying over the exact one from @types also does not work. So the version of the HLJS types I was looking at with the Ambient modules doesn't actually seem to be the official one... I was looking at someones fork. So this doesn't actually seem to work right now, even with the official package. What I'm trying to do - allow deep requires into the library: import core from 'highlight.js/lib/core.js'; Using code like (ambient modules): declare module 'highlight.js/lib/core.js' {
export = hljs;
} I'm getting this error:
It seems pretty clear from that TS understands what I want to do, but I don't know why it "which cannot be augmented." |
Can anyone weigh in on: I think this resolves the issue and allows importing the library in all the supported variants. It seems to work in my quick testing. |
Resolving with 10.1.1 |
@yyyc514 Thanks for the fix! I just migrated from @types/highlight.js to the types shipped with highlight.js v10.1.1. |
Ambient modules work just fine in @types though... I'd be fine with adding docs (if that's needed) but first I'd like to understand the actual problem here and if it isn't something we can just fix in the code. I thought that if I added a
How is our exact project setup? Code perhaps? You should be importing I tested all of these types of imports in a tiny TS project and they all worked:
|
I will try to provide a reproduction by tomorrow. |
Note that VSCode has a feature that pulls But I don't know whether this also affects pure TS projects. I just wanted to mention this because it may lead to issues. |
Well it probably shouldn't do that if the library already has a types file though... I dunno how all this stuff works exactly. |
It is explained in a little more detail here: I still don’t know why it works for you and why I have to add it to the types array. |
@yyyc514 https://github.com/P4sca1/hljs-ts |
I don't know the answer here someone from TS is goign to have to weigh in. It works if you start by loading the whole library:
Which is what I was seeing... but of course that's not a good test, oyu need to be able to load them individually, but that fails... yet I thought this exactly why @types existed and also that we should be able to do the same thing by bundling types with our library. So I feel like we still just don't understand how we're supposed to do this yet but if someone could clue us it it should "just work". |
I feel like you may need to add:
To your file if you aren't importing the ROOT of the library. This is also documented: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html |
This could break tree-shaking on some builds. One way to do it without a I think if we'd use the solution mentioned in my comment, we can fix this. It's basically this: mv index.d.ts core.d.ts
echo "import hljs from './core.js';\nexport = hljs;" > index.d.ts If |
I don't see any real solution here if it doesn't also solve for languages... Core is useless without also loading languages... am I missing something? And adding 180 + |
Just to be clear: This is not what I am proposing. |
Sorry wasn't putting words in your mouth, just that seems to be the logical conclusion... Just fixing index and core is only 10% solving the problem here. I didn't see a solution for languages in your message. I'm pretty sure the existing @types stuff doesn't handle core and languages either the way it currently stands... I really wish we had some TS experts to help us here on "best practices" vs "cute hacks", etc. |
Oh so maybe you did solve it and I just missed it hidden in the middle. Does this indeed solve everything because I'm not completely sure I follow "It's just that the ambient modules for the language files have to be loaded.".
...seems to work now... I Presume it would also work in core, no? |
I've implemented my proposal based on the current master branch here: This is untested. Maybe it works for the above-mentioned case. Edit: Then Edit2: |
I'm not such which "that case" is referring to...
So if you put it in lib you can't load core without issues but the languages still do not work? but the ambient modules were inside the core.d.ts, right? I'm not sure why that wouldn't work. I may have to try it myself.
I'm not sure I follow. I was suggesting that anyone USING our library would add the |
Yeah ambient modules don't seem to mix with top-level exports very well.... |
The case: https://github.com/P4sca1/hljs-ts
Yes. It doesn't seem to work likely because If the language ambient module is in a separate file, it could work. But then we cannot use the types of hljs in them because they are not exported (only the hljs constant). Importing them also does not work. Edit: These files:
declare module 'highlight.js/lib/languages/*' {
export default function(hljs?: unknown): unknown;
}
With that setup, this has no errors:
The parameter of |
ts file content:
after I run command
I got error
The version I use is 10.1.0.
Try to use 10.0.2 and it build successfully.
The text was updated successfully, but these errors were encountered: