question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `import type "mod"`

See original GitHub issue

Search Terms

type-only import

Suggestion

The new type-only import syntax supports most variations of import statement but not this one: import type 'mod' It may seem stupid because no type is actually imported?! But it’s in fact useful if mod contains some global declarations that you need.

Global declarations are currently only imported from: typeroots (@types/* by default) or types configs; and referenced modules.

~This syntax is even more useful because it can’t be emulated today. If you do import 'mod' then typing works but TS assumes you import the module for side-effects and will not erase it.~(EDIT: see triple-slash comment)

Use Cases

I encountered this today. One package I use provides some dependency injection and basically defines the following module augmentation: inject(x: 'myType'): MyType which is a strongly typed overload of inject<T = unknown>(x: string): T. Inject itself comes from another library so code that wants to use this import needs to do this:

import { inject } from 'other-lib';
import type 'my-lib';

let my = inject('myType'); // correctly typed as MyType

Current work-arounds include:

  • Importing a type import { MyType } from 'my-lib' but if you don’t use it in code it’ll be a warning;
  • Import without type import 'my-lib' but it won’t be erased;
  • ~Add this lib to typeroots or types and do nothing (might be better in some cases).~ (EDIT: not really working, see my comment below for an alternative solution.)
  • Use /// <reference types="mod" />, which works but I think most people have forgotten about? (I have 😜)

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
ExE-Bosscommented, Feb 14, 2020

You can use Triple‑Slash Directives to achieve this:

/// <reference types="mod"/>
3reactions
xiaoxiangmoecommented, Jul 28, 2020

try

import type { } from 'my-lib'
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Modules - TypeScript
Modules import one another using a module loader. ... With TypeScript 3.8, you can import a type using the import statement, or using...
Read more >
Adding `import` statement to `global.d.ts` destroys type and ...
Using import makes the file a module and no longer an ambient type declaration file. To fix this, use the exotic import function...
Read more >
How can I import type definition structs in modules ... - Reddit
Trying to pick up Rust and struggling with the following , namely how to module import/export in Rust. I would like to have...
Read more >
How To Use Modules in TypeScript | DigitalOcean
Next, add TypeScript as a development dependency: ... First, you will try out the correct syntax to import this kind of module by...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
In HTML, this is done by adding type="module" to the <script> tag. ... from "mod" is not equivalent to import defaultExport from "mod"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found