microsoft / microsoft/TypeScript

request: add accompanying `importsNotUsedAsValues` option for declaration imports

オープン
#44,519 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Suggestion

I'm looking for the ability to instruct the compiler to preserve imports as 'side-effecting' for declaration files. I'm framing this request in terms of the existing importsNotUsedAsValues option for sake of clarity but this is for declarations only.

I suspect it may require a new accompanying option to importsNotUsedAsValues called, .e.g., declarationImportsAsSideEffects

🔍 Search Terms

  • declarations side effect
  • importsNotUsedAsValues declarations
  • import side effect declarations

✅ Viability Checklist

  • 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Based on the existing importsNotUsedAsValues behaviour, imports are emitted to .js to varying degrees. This proposal would ensure that there are accompanying imports within emitted .d.ts files to the same degree. For imports that are value-only and preserved, a "side effecting" import './a-module' would be emitted; others would remain as is

📃 Motivating Example

// plugins/my-plugin.ts
declare global {
  export interface AppPlugins {
    myPlugin: {some: 'value'}
  }
}

export default function() { ... }
// plugin-loader.ts

declare global {
  export interface AppPlugins {
    corePlugin: {some: 'otherValue' }
  }
}

import myPlugin from './plugins/my-plugin'
// import './plugins/my-plugin'.   <---- currently required

const plugins: Plugin[] = ...
export default plugins

Compiling yields (tested 4.3.2 with importsNotUsedAsValues='preserve')

// plugin-loader.d.ts

declare global {
  export interface AppPlugins {
    corePlugin: {some: 'otherValue' }
  }
}

// import './plugins/my-plugin'  // <---- would be emitted with proposed change

declare const plugins: Plugin[];
export default plugins;

Currently, if another module were to import plugin-loader:

// app.ts
import plugins from './plugin-loader'

type PluginNames = keyof AppPlugins  //  'corePlugin'

Note that the augmentation type (global/module) doesn't matter, just that they're included

💻 Use Cases

  • What do you want to use this for?
    Enable plugin architectures where augmentations can be reliably expected to have been reached

  • What workarounds are you using in the meantime?
    "Shadow" side-effect imports to ensure declarations include import

What shortcomings exist with current (workaround) approach?

  • Redundant, confusing to new eyes etc. In my particular use case where such code will be authored by plugin creators, any decreased DX is particularly unappealing. Worth mentioning that any missed shadow import in the chain would lead to the augmentation going unnoticed so this workaround would snowball

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、既存の importsNotUsedAsValues の動作と、宣言ファイルがどのように出力されるかを確認してください。値のみの import が生成された .d.ts ファイルでどのように表現されるかを追跡してください。対応するオプションによって宣言内の必要な副作用 import が保持され、動作が動機となったプラグイン拡張の例をカバーできれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。