-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Can't use typeof for an ambient external module type. #2346
Comments
I found this in the handbook: It says that ExternalModule.d.ts: declare module "ExternalModule"
{
export var member: number;
} App.ts import ExternalModule = require("ExternalModule");
module Test
{
if (false)
var externalModuleInstance: typeof ExternalModule = require("SomeExternalModule");
} emits var Test;
(function (Test) {
if (false)
var externalModuleInstance = require("SomeExternalModule");
})(Test || (Test = {})); Seems like I would have still found it easier to naturally apply |
Seems like import can be used to only import the type (when only use in a type position) but if used as a value would import the module itself. I'm not sure how many TypeScript developers are actually aware of this (perhaps this feature was added, or at least documented, only recently?) as this is quite an ambiguous use of both the keyword import and require. You're definitely not the first to be bitten by this. It is subtle but there are good reasons for it. We've recently been talking about potential alternatives to make this scenario easier/clearer. |
Hi, thanks for your reply! It's not every day that I discover a way to do something that I thought was impossible for literally years now. EDIT: I opened a new issue with a proposal for an alternative syntax: #2357. |
I need to get type information for a CommonJS module that's dynamically (and sometimes conditionally) loaded within the scope of an internal module. Some background on why this is important (and the hoops I go through to work around this restriction today) here: #2327 (comment)
(TL;DR: my current workaround is to transform the external module declaration file to an internal module declaration file, using several regular expressions, here's one I did for node.d.ts (with the RegExps I used in the comments): https://github.com/rotemdan/lzutf8.js/blob/master/LZUTF8/Library/Dependencies/node-internal.d.ts)
This works (with correct intellisense):
But this:
or even this:
Results in
The text was updated successfully, but these errors were encountered: