-
Notifications
You must be signed in to change notification settings - Fork 12.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
Add the possibility to import Types from global loaded Librarys #57019
Comments
I don't understand what you're asking for. What makes this import "global" ? What's the emitted code supposed to be? If you have a CJS library, how are you loading it from a browser without using a bundler? |
Iβm guessing theyβre loading the TS API globally (using a |
If so, then that was #53570, which we decided not to do. |
I'm developing my application without using any bundler as a Project wich is using ESM modules. So I tried to imporrt typescript (or also other packages wich are only available as CJS), but it will fail. So I load these modules globaly (via a scrit tag in my main html file), but then I can not use the tyes in my ts file. This is the issue. I can then use "import type", but then I can use the import only as a type definition, but I also need to access objects from it. This will work at runtime since the file is loaded globally |
I would love to only use ESM, but also TS is not available as a ESM build |
the emitted code should be the same as with import type, remove the import, leave the rest. what is the typescript npm packge? is it not CJS? I can load this directly via scrit tag in browser, and I can import it in vscode in my file, but the import will only work if I use a bundler |
typescript.js creates a
to represent that that's happening |
Is this now a question? Cause I can not get this to work, or is this an issue? |
I guess one thing you could do is have a separate file like export * from "typescript";
export as namespace ts; |
Though I guess that gives you this error...
|
Duh: import typescript = require("typescript");
export = typescript;
export as namespace ts; |
It's a safety measure - there's |
Isn't then my suggestion a valid feature request? To use a global JS library with type information, I need to create an extra file, create a bunch of code, add an compiler switch wich may be deprecated at some time... |
It is, and we can keep it open - I just realistically don't know how much traction it will get, versus how much the cost is of adding new syntax and supporting it. The workaround isn't great, but it is possible and not a huge burden, and the number of secretly-global modules is going to get rarer. |
Yeah, thanks for the solution. Already tested it, and works nicely. |
@jogibear9988 What about this one? Put it in a seperate import ts = require('typescript');
declare global {
namespace globalThis {
export { ts }
}
} |
Yeah, also works. And for me, easier to understand |
π Search Terms
import types global
β Viability Checklist
β Suggestion
In browser environemnt you often need to load librarys, wich are developed as ESM modules ans with ESM types globaly, cause they are only delivered as CJS, for example typescript, see: #57010
There should be a way, so I can import typescript and use it, without loading the library (like import type), but there should be no error if I use instances from the import.
for example at the moment it is like this:
will error cause the import is only imported as type
maybe something like this:
or
would be possible?
The text was updated successfully, but these errors were encountered: