Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper type to extract valid keys from container instance #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ecoronadosc
Copy link

@ecoronadosc ecoronadosc commented Aug 30, 2024

Given a container instance, I want to have interfaces that require a valid type from the container.

I could create MyServices type of valid keys, pass it to the container and then require a keyof MyServices.. but it's easier to allow the container to build the types and extract it from it:

interface MyServices {
  foo: number;
  bar: string;
}
type ValidServices = keyof MyServices;

interface Plugin {
  key: ValidServices;
}

const container = new Container<MyServices>({})

vs

const container = new Container({})

type ValidServices = ContainerKeys<typeof container>;

interface Plugin {
  key: ValidServices;
}


Similar to other helpers such as:
https://react-typescript-cheatsheet.netlify.app/docs/react-types/componentprops/#infer-component-props-type

Given a container instance, I want to have interfaces that require a valid type from the container.

I could create MyServices type of valid keys, pass it to the container and then require a keyof MyServices.. but it's easier to allow the container to build the types and extract it from it:

```
interface MyServices {
  foo: number;
  bar: string;
}
type ValidServices = keyof MyServices;

interface Plugin {
  key: ValidServices;
}

const container = new Container<MyServices>({})

```

vs

```
const container = new Container({})

type ValidServices = ContainerKeys<typeof container>;

interface Plugin {
  key: ValidServices;
}


```
type ReturnTypeOfFactories<T> = T extends { factories: infer R } ? R : never;

/**
* Helper type for extracting valid keys from a container instance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a usage example here, please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants