microsoft / microsoft/TypeScript

add an option to report an error when `Symbol.dispose`/`Symbol.asyncDispose` objects are used without the `using` keyword

Ouverte
#63,597 6 commentaires 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔍 Search Terms

dispose using error

✅ Viability Checklist
⭐ Suggestion

i was surprised to learn that using disposables without the using keyword are not reported as an error:

const foo = {
  [Symbol.dispose]: () => {
    console.log("disposed"); // never called
  },
};

{
  const bar = foo;
  console.log(bar);
}
📃 Motivating Example

the documentation compares it to python with statements. but the whole point of context managers is to enforce that an object is always properly cleaned up. if you forget to use with, then the object's setup is never run so its cleanup doesn't need to run either.

since this is not the case with the using keyword, it should be a type error to use it in an unsafe way

💻 Use Cases

i have an API that i need to log out of when i'm finished using it, otherwise the session remains active

const login = () => {
    // ...
    return {
        [Symbol.asyncDispose]: () => {
            // important cleanup code that MUST be run no matter what
        }
    }
}

in python i would do this with a context manager, and i'd be confident that the cleanup code would always be executed. it's not up to the caller to remember to use the with statement. however in typescript, the caller could easily do this by mistake:

const sesion = await login()
// cleanup code never gets called

this is extremely surprising behavior coming from a language with a similar feature. is this is intentional, the docs should have a clear warning that Symbol.dispose and Symbol.asyncDispose functions have no guarantee to actually be called.

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

Aucun fichier d’implémentation ni test n’est indiqué. Commencez par la documentation liée des notes de version de TypeScript 5.2 et par les exemples synchrones et asynchrones de l’issue, puis déterminez le comportement attendu du diagnostic et de l’option pour les objets disposable utilisés sans using. La tâche est terminée lorsque l’utilisation incorrecte demandée est signalée sans modifier le JavaScript généré.

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

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
42/100

Recevez les nouvelles issues par e-mail

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