Defines the contract for objects that can report their disposal state.
This interface does NOT extend Disposable because it represents objects that can
report whether they have been disposed, but may not necessarily be disposable themselves.
This separation allows for:
Objects that are disposed by external managers
State observers that need to check disposal status
Utility functions that work with disposal state without requiring disposal capability
Use this interface when you need to check if something has been disposed but don't
need the ability to dispose it directly.
Example
// A class that can be disposed but implements DisposableAware for state checking classStatefulResourceimplementsDisposableAware { private_disposed = false; getwasDisposed(): boolean { returnthis._disposed; } dispose(): void { if (!this._disposed) { // Perform cleanup this._disposed = true; } } doWork(): void { if (this.wasDisposed) { thrownewError('Cannot work on disposed object'); } // Perform work... } }
// A utility function that only needs to check disposal state functionisResourceStillUsable(resource: DisposableAware): boolean { return !resource.wasDisposed; }
Gets a value indicating whether the object has been disposed.
This property should return true once the object has been disposed
and should remain true for the lifetime of the object. It should
never transition back to false after being set to true.
Returns
true if the object has been disposed; otherwise, false.
Example
functionsafeOperation(resource: DisposableAware): void { if (resource.wasDisposed) { console.warn('Resource already disposed, skipping operation'); return; } // Safe to use resource performOperation(resource); }
Defines the contract for objects that can report their disposal state.
This interface does NOT extend
Disposable
because it represents objects that can report whether they have been disposed, but may not necessarily be disposable themselves. This separation allows for:Use this interface when you need to check if something has been disposed but don't need the ability to dispose it directly.
Example