microsoft / microsoft/TypeScript
Cannot use type side of a namespace in JsDoc after `declare global ...` workaround for UMD globals
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
I hit this while writing an electron app using checkJs where you can both require code in (thus making your file a module), and there may also be script tags loading code in the HTML for the app. For example, by main page has the below as it is using D3. Thus the 'd3' object is available globally.
<script src="node_modules/d3/dist/d3.js"></script>
<script src="./app.js"></script>
Trying to use the global D3 in my app.js however results in the error 'd3' refers to a UMD global, but the current file is a module..., so I've added the common workaround below to avoid this via a .d.ts file.
import {default as _d3} from 'd3';
declare global {
// Make the global d3 from directly including the bundle via a script tag available as a global
const d3: typeof _d3;
}
When the above .d.ts code is present (and only when), JSDoc gives an error on trying to use types from the namespace, i.e. the below code
/** @type {d3.DefaultArcObject} */
var x;
Results in the error Namespace '"./@types/d3/index".d3' has no exported member 'DefaultArcObject'. Yet the below TypeScript continues to work fine:
var x: d3.DefaultArcObject;
The below also continues to work fine in JavaScript, but is kind of ugly and a pain to have to repeat (especially if you need to use a lot of type arguments)
/** @type {import('d3').DefaultArcObject} */
var x;
Personally I'd rather not have to do the .d.ts workaround at all and just be able to use the d3 global in my modules (see the highly controversial #10178). That not being the case, JsDoc should be able to access the types still with the workaround in place.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
.d.ts のグローバル宣言、app.js、および d3.DefaultArcObject への JSDoc 参照を使って、まず checkJs のケースを再現します。失敗する JSDoc の検索と、機能する TypeScript の型注釈および import('d3') の形式を比較します。グローバル宣言を通じて、報告されたエラーなしに名前空間の型が解決されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- d3, electron, javascript, typescript
- 領域
- compilers, developer-experience
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100