---
meta:
 - name: keywords
   description: api typescript node.js documentation OnInit interface
---
# OnInit - @tsed/di

## Usage

```typescript
import { OnInit } from "@tsed/di";
```

> See [/packages/di/src/common/interfaces/OnInit.ts](https://github.com/tsedio/tsed/blob/v8.27.0/packages/di/src/common/interfaces/OnInit.ts#L0-L0).

## Overview

```ts
interface OnInit {
    $onInit(): Promise<any> | void;
}
```

<!-- Description -->

## Description

Lifecycle hook interface for initialization logic.

Implement this interface to execute custom initialization code when a provider instance is created by the DI container.
The `$onInit` method is called automatically after the instance is constructed and dependencies are injected.

### Usage

```typescript
import {Injectable, OnInit} from "@tsed/di";

@Injectable()
class MyService implements OnInit {
  async $onInit() {
    // Initialize database connection, load configuration, etc.
    await this.connect();
  }
}
```

<!-- Members -->

## $onInit

```ts
$onInit(): Promise<any> | void;
```
