Skip to content

Suggestion: Add support for dispose pattern #16459

@sheiidan

Description

@sheiidan

This suggestion propose adding support for the good known and familiar Dispose pattern (e.g. 'using' statement like we have in C#).
Since there's no class destructor in ts/js there's a need for a resource management (or cleanup) pattern in order to do smart/auto handling for releasing resources and cleanup our code.
This TS with USING statement:

export class MyDisposable {
    public dispose() {

    }
}

using(let x = new MyDisposable()) {
    console.log("Hi");
}

should be transpiled into:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MyDisposable = (function () {
    function MyDisposable() {
    }
    MyDisposable.prototype.dispose = function () {
    };
    return MyDisposable;
}());
exports.MyDisposable = MyDisposable;
var x;
try {
    x = new MyDisposable();
    console.log("Hi");
}
finally {
    if (typeof x !== "undefined") x.dispose();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions