There's a few other issues that touch on something similar (https://github.com/Microsoft/TypeScript/issues/5098, https://github.com/Microsoft/TypeScript/issues/278), but is there a way to explicitly import only type information from an external module? I imagine something like below, where there's a reserved word used to modify an existing import:
import type * as ts from 'typescript'
import type ts = require('typescript')
ts.parseJsonConfigFileContent(...) // Error, type information only.
var TS: ts = require('typescript')
ts.parseJsonConfigFileContent(...) // Works great.
This construct would import type information only, and any runtime usage of the namespace in the current module could be considered an error. This is useful in a few place (E.g. ts-node and tsconfig) where I need the type information from TypeScript, but I also need to guarantee that I don't accidentally import it as a dependency in the compiled output. Not sure if others have a similar use-case, but the reverse at least seems to be an issue (importing for side-effects only, which is optimized out by the compiler currently).
So you're not going for eliding modules, right? You're specifically trying to make it so that users don't accidentally use values withing a module?
but I also need to guarantee that I don't accidentally import it as a dependency in the compiled output
I tend to use _ prefix for _type import only_ scenarios. e.g.
import _atomUtils = require('../main/atom/atomUtils');
var atomUtils: typeof _atomUtils;
Use case lazy loading Or I might have misunderstood the question :rose:
@basarat Absolutely correct, that's the same use-case I have with ts-node too. I wanted the TypeScript compiler type information, but the TypeScript compiler is lazy loaded depending on the compiler string the user specifies.
@DanielRosenwasser Yes, eliding modules works. But I need to be explicitly sure that I haven't accidentally used the value before it's been defined (E.g. guarantee that require('typescript') does not get emitted).
Edit: Also, yes, _ works and is a reasonable workaround with the current behaviour. But making this explicit also fixes the other side of the coin - where I need an import but aren't using it anywhere (E.g. a polyfill or something else relying solely on side-effects).
see #2812 for more details.
Just realised Flow actually handles this and the syntax is identical: http://flowtype.org/blog/2015/02/18/Import-Types.html.
Most helpful comment
I tend to use
_prefix for _type import only_ scenarios. e.g.Use case lazy loading Or I might have misunderstood the question :rose: