Skip to content

Typescript

Francisco Ruiz edited this page Aug 27, 2024 · 1 revision

Typescript

Packet.js is built on top of TypeScript and supports TypeScript projects. This section will describe how to use Packet.js with TypeScript and Generic Types.

get<T>(key, options)

The get<T>() method returns the instance of the registered service. So, to define the type of the service, you can do the following:

import { Service } from './@types';

const service = container.get<Service>('Service');

getFactory<T>(key, options)

Similar to the get<T>() method, the getFactory<T>() method returns an instance for the registered service, and the type of the service is defined using the getFactory<T>() method.

import { Service } from './@types';

const service = container.getFactory<Service>('Service');

getAll<T>(options)

The getAll<T>() method returns an object with the following structure:

{
  Service1: () => { // Logic for the registered service 1 };
  Service2: () => { // Logic for the registered service 2 };
  ...
  ...
}

So, to define the type of each service, you can do the following:

import { Service1, Service2 } from './@types';

interface ServicesMap {
  Service1: () => Service1; // Declare the type for the service 1
  Service2: () => Service2; // Declare the type for the service 2
  ...
  ...
}

Then we can use it like this:

const services = container.getAll<ServicesMap>();

getProps<T>()

The getProps<T>() method returns an object with the properties registered in the container. So, to define the type of the properties, you can do the following:

import { Props } from './@types';

const props = container.getProps<Props>();

1. Getting Started

2. Core Concepts

3. Middleware

4. Caching and Memoization

5. Typescript

6. Considerations

7. API Reference

8. Examples and Use Cases

9. Contributing

We welcome contributions from the community! Whether it's reporting a bug, suggesting a feature, or contributing code, your involvement is key to making Packet.js better. Please check out this section for more details.

10. Support

If you encounter any issues or bugs while using packetjs, or if you have any feature requests, please feel free to open an issue in our GitHub repository. You can report issues, track progress, and engage in discussions with the community via the following link:

11. FAQ

If you have any questions or need further assistance, feel free to reach out via the discussion board.

12. License

Clone this wiki locally