dtor
v0.1.2
Published
Very small library which adds a deconstructor to classes in typescript
Downloads
2
Readme
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>;