TypeScript now supports Disposables: objects that have some "dispose" behavior when they go out of scope.
It's important to use using
and not const
/let
when using those objects, otherwise the automatic cleanup does not happen and you would leak resources.
In this case we are opening a file through an API that expects to be disposed, but we are not using it as such. The readConfig
function will thus leak resources associated to the opened file.
declare function openFile(url: string): Disposable & MyFileAPI; function readConfig() { const configFile = openFile("./configFile"); return configFile.contentsAsJson(); }Pass Cases
In this case we are properly disposing the file resource.
declare function openFile(url: string): Disposable & MyFileAPI; function readConfig() { using configFile = openFile("./configFile"); return configFile.contentsAsJson(); }Additional Info
This is the equivalent to require-await
, but for Disposables rather than Thenables.
auvred, Yona-Appletree, maxdavidson, kirkwaiblinger, eldiGH and 30 more
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4