-
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
import type
should not need runtime modules support
#44549
Comments
I see, thanks, and sorry for the duplicate. Although it leaves unanswered the question of how to reference modules, and in particular type modules from script :( |
I stand corrected by @Jamesernator. |
This isn't entirely true, although it's kinda awkward without proper import. Basically you can use: type MkdirProto = typeof import("fs")["mkdir"]; This isn't too bad to be honest, although becomes tedious if you want a lot of types as there's no destructuring or such to speak of in the type system. So you'll need a |
Ah excellent thanks. I saw that from some other comments but wasn't aware this syntax wasn't considered a module import as opposed to import type. Is this documented somewhere ? |
Suggestion
🔍 Search Terms
import type
module
--outFile
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Intuitively I expect
import type
to be a compile-time only construct. This is currently not really the case, because usingimport type
triggers the compiler into thinking we are using ES modules.📃 Motivating Example
Consider this
test.ts
:This to compile to a
ŧest.js
which is more or less:Which does not use ES modules. Nevertheless, the compiler refuses to compile this if modules are disabled, e.g.
tsc test.ts --outFile test.js --target es2020
fails with:Which makes me think the compilers treats
import
andimport type
the same way.💻 Use Cases
This is clearly not a showstopper, more of an optimization. One use-case is trying to build simple Node.JS scripts, written in typescript as a single file (with the
--outFile
flag), in order to deploy each script as a single javascript file, that I can run natively on a Node.JS server with no typescript support (no tsc or ts-node installed).Since the script only has dependency to node's standard library, I was not expecting typescript to refuse to compile as a single file.
We can work arround by defining a
tsconfig.json
and building in a directory, then extracting the js file generated in that build/ dir. And I realize there are a lot of issues around here about packaging as a single file. But in this case, there is no concatenation required at all. This is just aboutimport type
triggering typescript into thinking we are using modules, when we really aren't.As I said, this is clearly not critical, and I have no idea if this would be easy to implement. So it's really just a suggestion :)
The text was updated successfully, but these errors were encountered: