microsoft / microsoft/TypeScript

Node module dependencies that use a triple-slash directive to reference libs poison the global scope

Ouverte
#33,111 5 commentaires 20 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

In Discussion Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

TypeScript Version: 2.x, 3.x

Search Terms: lib.dom.d.ts pollutes global scope, name is defined as never, event is defined as Event | undefined

See: https://github.com/AaronFriel/is-test/blob/4e235decffdfd3c137ea066f8462d57d2fc8d0f2/src/index.ts at the commit hash linked.

Expected behavior:

Given a tsconfig.json with lib: [], I would expect the following code to fail to build due to $variable not defined errors:

import is from '@sindresorhus/is';

function main() {
  console.log(name);
  console.log(event);
  console.log(caches);
  console.log(applicationCache);
  console.log(parent);
}

Actual behavior:

See: https://github.com/AaronFriel/is-test/blob/master/src/index.ts for a particularly egregious example of this. It builds without any type errors. When running npm run start, it will throw a runtime error on the first console.log. If you remove that line, the next one fails, and so on, for the next 700+ lines.

General purpose libraries often need to be written to handle multiple versions of ES (ES5, ES6, ES7...) and multiple host environments (e.g.: DOM, ScriptHost, WebWorker). But right now they are faced with a choice:

A: Use a triple slash directive to import types that they use, and poison the user's global scope, causing nearly one thousand variable names to be silently accepted as defined without any type checker errors.

B: Remove the triple slash directives and require that users use the --skipLibCheck option or equivalent in their config file or include the required libs in their compiler options. Otherwise the library will fail to type check.

This is a choice that library authors would quite reasonably not want to make.

Potential solutions:

  1. Triple slash directives should import types only in dependencies.

    This seems tricky to implement, I think.

  2. There should be a types-only version of the major lib files, so that general-purpose utility packages can reference those instead.

    This seems less tricky: extract all the types from the dom lib a new dom-types lib, and use a directive to reference the types. Then, library authors can triple-slash reference dom-types and get only the types. Has the advantage of not causing any compatibility issues, and library authors can opt in to the types-only package.

  3. ???

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire le comportement avec l’exemple src/index.ts lié et un tsconfig.json utilisant lib: []; examinez l’impact des directives triple-slash et de lib.dom.d.ts sur la vérification des types des dépendances. Le travail est considéré comme terminé lorsque les globales non déclarés de l’exemple produisent des erreurs de type sans obliger les auteurs de bibliothèques à supprimer les types auxquels ils font référence ; aucun fichier de test spécifique n’est indiqué.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.