microsoft / microsoft/TypeScript

Add `import type "mod"`

Open
#36,812 12 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue proposes supporting import type "mod" for loading global declarations without runtime emission, with an example involving module augmentation. No implementation files or tests are identified in the issue, so first locate the import parser, type-checking, and emit tests; done means the syntax type-checks and produces the intended erased output.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.