tc39 / proposal-explicit-resource-management

ECMAScript Explicit Resource Management
https://arai-a.github.io/ecma262-compare/?pr=3000
BSD 3-Clause "New" or "Revised" License
755 stars 30 forks source link

Type checking linear usage of disposables #90

Open littledan opened 2 years ago

littledan commented 2 years ago

Resources which should be disposed of leads to potential room for type checkers (or linters) to help JS developers! I imagine this doesn't escape the author of this proposal, a top expert in JS type checking, but I'm curious about your thoughts in this area. The property that occurs to me is enforcing some part of "linearity", which I guess is not at all easy to check for, but I'm still wondering if it may be practical to implement part-way.

I wonder if type checkers could enforce that, you call a function which returns a disposable (e.g., DisposalStack.prototype.move), the caller should either use that in a using declaration or cause it to "escape" somehow or other (e.g., using it as an argument in a function, or storing it as a property in an object, but not randomly ignoring the return value of the function).

rbuckton commented 2 years ago

We've considered something similar for Promises in TypeScript, and may do something similar for both in the future. The typescript-eslint plugin for ESLint has a no-floating-promises rule for Promises currently, so something like this is definitely possible.