-
Couldn't load subscription status.
- Fork 0
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.
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');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');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>();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>();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.
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:
If you have any questions or need further assistance, feel free to reach out via the discussion board.