proposal-explicit-resource-management icon indicating copy to clipboard operation
proposal-explicit-resource-management copied to clipboard

Type checking linear usage of disposables

Open littledan opened this issue 3 years ago • 1 comments

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).

littledan avatar Sep 03 '22 12:09 littledan

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.

rbuckton avatar Sep 07 '22 20:09 rbuckton