Skip to content
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 information only #5556

Closed
blakeembrey opened this issue Nov 6, 2015 · 6 comments
Closed

Import type information only #5556

blakeembrey opened this issue Nov 6, 2015 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@blakeembrey
Copy link
Contributor

There's a few other issues that touch on something similar (#5098, #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).

@DanielRosenwasser
Copy link
Member

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?

@basarat
Copy link
Contributor

basarat commented Nov 6, 2015

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 🌹

@blakeembrey
Copy link
Contributor Author

@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).

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2015

see #2812 for more details.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 6, 2015
@mhegazy mhegazy closed this as completed Nov 6, 2015
@blakeembrey
Copy link
Contributor Author

Just realised Flow actually handles this and the syntax is identical: http://flowtype.org/blog/2015/02/18/Import-Types.html.

@philkunz
Copy link

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants