dtor
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Experimental TypeScript Deconstructor

Requires TypeScript 0.5.2 or newer

Example tsconfig.json

{
    "compilerOptions": {
        "lib": ["esnext", "dom"],
        "target": "es2015",
        "module": "commonjs",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "outDir": "build",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "declaration": true
    }
}

Example using with async deconstructors

import type { AsyncDeconstructable } from 'dtor'
import { Deconstructor } from "dtor";

class SomeExample {
    public constructor() {
        console.log("constructed");
    }

    @Deconstructor()
    public async asyncDeconstructor() {
        await new Promise((r) => setTimeout(r, 2000));
        console.log("deconstructed");
    }
}

void (async () => {
  await using cls = new SomeExample() as AsyncDeconstructable<SomeExample>;
})()

Example using with sync deconstructors

import type { Deconstructable } from 'dtor'
import { Deconstructor } from "dtor";

class SomeExample {
    public constructor() {
        console.log("constructed");
    }

    @Deconstructor()
    public deconstructor() {
        console.log("deconstructed");
    }
}

using cls = new SomeExample() as Deconstructable<SomeExample>;

Without this library

class SomeExample {
    public constructor() {
        console.log("constructed");
    }

    [Symbol.dispose]() {
        console.log("deconstructed");
    }
}

using cls = new SomeExample() as Deconstructable<SomeExample>;

Readme

Keywords

none

Package Sidebar

Install

npm i dtor

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

4.25 kB

Total Files

5

Last publish

Collaborators

  • blu3beri