microsoft / microsoft/TypeScript
Add `import type "mod"`
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
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 (EDIT: see triple-slash comment)import 'mod' then typing works but TS assumes you import the module for side-effects and will not erase it.
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(EDIT: not really working, see my comment below for an alternative solution.)typerootsortypesand do nothing (might be better in some cases).- 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 を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue では、実行時の出力を発生させずにグローバル宣言を読み込むための import type "mod" のサポートを提案しており、モジュール拡張を伴う例が示されています。issue では実装ファイルやテストが特定されていないため、まず import パーサー、型チェック、emit のテストを見つけてください。構文が型チェックを通過し、意図した消去済みの出力を生成すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100