Closed loynoir closed 1 year ago
The difficult thing is that a runtime error for const x = <disposable>
would be wholely backwards incompatible - it means you can't pass a disposable anywhere.
For example imagine this code:
function incrementCounter(arg) {
const foo = arg;
foo.counter = (foo.counter ?? 0) + 1;
}
using disposable = {[Symbol.dispose]() {}};
incrementCounter(disposable);
incrementCounter(disposable);
On the second line the alias is completely harmless to the program running and harmless to the conceptual flow of the disposable - so the runtime preventing it would make the feature really hard to use without crashing!
This is a dupe to #159
After more in-depth discussion on that thread, it started to be apparent to me the reason behind the choice of returning Disposable
type is to maintain drop-in level compatibility for existing functions. You can upgrade your existing function to return a Disposable
without affecting any old code using the old dispose mechanism.
This compatibility however, created a unique challenge in linting code for disposal mistake like forgetting to write using
. Read more in the "Linter failure" section of this thread. I'd like to hear from @rbuckton on a solution to this problem.
Closed as seems duplicated.
Wow, that thread is really long.
Related https://github.com/typescript-eslint/typescript-eslint/issues/7160
I think current implement might be insecure when
const x = { [Symbol.dispose]() {} }
instead ofusing x = { [Symbol.dispose]() {} }
It might better if can avoid this at language scope.