microsoft / microsoft/TypeScript
add an option to report an error when `Symbol.dispose`/`Symbol.asyncDispose` objects are used without the `using` keyword
Personne n'a encore pris cette issue.
- 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
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
- 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
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