Ir al contenido

useDisposables

Esta página aún no está disponible en tu idioma.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useDisposables": "error"
}
}
}
}

Detects a disposable object assigned to a variable without using or await using syntax.

Disposable objects, which implements Disposable or AsyncDisposable interface, are intended to dispose after use. Not disposing them can lead some resource or memory leak depending on the implementation.

example1.ts
function createDisposable(): Disposable {
return {
[Symbol.dispose]() {
// do something
},
};
}
const disposable = createDisposable();
/example1.ts:9:7 lint/nursery/useDisposables  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Disposable object is assigned here but never disposed.

7 │ }
8 │
> 9 │ const disposable = createDisposable();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 │

The object implements the Disposable interface, which is intended to be disposed after use with using syntax.

Not disposing the object properly can lead some resource or memory leak.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Add the using keyword to dispose the object when leaving the scope.

7 7 }
8 8
9 - const·disposable·=·createDisposable();
9+ using·disposable·=·createDisposable();
10 10

example2.ts
class MyClass implements AsyncDisposable {
async [Symbol.asyncDispose]() {
// do something
}
}
const instance = new MyClass();
/example2.ts:7:7 lint/nursery/useDisposables  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Disposable object is assigned here but never disposed.

5 │ }
6 │
> 7 │ const instance = new MyClass();
^^^^^^^^^^^^^^^^^^^^^^^^
8 │

The object implements the AsyncDisposable interface, which is intended to be disposed after use with await using syntax.

Not disposing the object properly can lead some resource or memory leak.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Add the using keyword to dispose the object when leaving the scope.

5 5 }
6 6
7 - const·instance·=·new·MyClass();
7+ await·using·instance·=·new·MyClass();
8 8

example3.ts
function createDisposable(): Disposable {
return {
[Symbol.dispose]() {
// do something
},
};
}
using disposable = createDisposable();
example4.ts
class MyClass implements AsyncDisposable {
async [Symbol.asyncDispose]() {
// do something
}
}
await using instance = new MyClass();