You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add the possibility to import Types from global loaded Librarys #57019
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:
import type ts form 'typescript'
const a = ts.SyntaxKind.TaggedTemplateExpression
will error cause the import is only imported as type
maybe something like this:
import global type ts form 'typescript'
const a = ts.SyntaxKind.TaggedTemplateExpression
or
import global ts form 'typescript'
const a = ts.SyntaxKind.TaggedTemplateExpression
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 <script> tag) and want those globals to be visible at compile time, rather than resolving it as a module.
I’m guessing they’re loading the TS API globally (using a <script> tag) and want those globals to be visible at compile time, rather than resolving it as a module.
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 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?
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
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.
Activity
RyanCavanaugh commentedon Jan 11, 2024
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?
fatcerberus commentedon Jan 12, 2024
I’m guessing they’re loading the TS API globally (using a
<script>tag) and want those globals to be visible at compile time, rather than resolving it as a module.jakebailey commentedon Jan 12, 2024
If so, then that was #53570, which we decided not to do.
jogibear9988 commentedon Jan 12, 2024
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
jogibear9988 commentedon Jan 12, 2024
I would love to only use ESM, but also TS is not available as a ESM build
jogibear9988 commentedon Jan 12, 2024
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
RyanCavanaugh commentedon Jan 12, 2024
typescript.js creates a
tsglobal and, if running in a CJS context, exports that. So what you need is just a .d.ts file that saysto represent that that's happening
jogibear9988 commentedon Jan 13, 2024
Ryan Cavanaugh (@RyanCavanaugh)
I get this error when I try this:
jogibear9988 commentedon Jan 14, 2024
Is this now a question? Cause I can not get this to work, or is this an issue?
DanielRosenwasser commentedon Jan 30, 2024
I guess one thing you could do is have a separate file like
ts-globalizer.d.tsin your project that looks like this:DanielRosenwasser commentedon Jan 30, 2024
Though I guess that gives you this error...
DanielRosenwasser commentedon Jan 30, 2024
Duh:
Playground Link
jogibear9988 commentedon Jan 30, 2024
Will also not work:
DanielRosenwasser commentedon Jan 30, 2024
It's a safety measure - there's
allowUmdGlobalAccessto get around it. Ryan Cavanaugh (@RyanCavanaugh) has not tried to mow that one down in recent deprecations. 😄jogibear9988 commentedon Jan 30, 2024
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...
DanielRosenwasser commentedon Jan 30, 2024
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.
jogibear9988 commentedon Jan 30, 2024
Yeah, thanks for the solution. Already tested it, and works nicely.
whzx5byb commentedon Jan 31, 2024
Jochen Kühner (@jogibear9988) What about this one? Put it in a seperate
.d.tsfile.jogibear9988 commentedon Jan 31, 2024
Yeah, also works. And for me, easier to understand