microsoft / microsoft/TypeScript
Cannot use type side of a namespace in JsDoc after `declare global ...` workaround for UMD globals
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire le cas checkJs en utilisant la déclaration globale dans .d.ts, app.js et la référence JSDoc à d3.DefaultArcObject. Comparez la recherche JSDoc qui échoue avec l’annotation TypeScript fonctionnelle et la forme import('d3'). C’est terminé lorsque le type de l’espace de noms se résout via la déclaration globale sans l’erreur signalée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- d3, electron, javascript, typescript
- Domaine
- compilers, developer-experience
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100