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

feat(types): support limiting/type checking entry names #115

Merged
merged 1 commit into from
Nov 1, 2018

Conversation

ethanresnick
Copy link
Contributor

@ethanresnick ethanresnick commented Oct 31, 2018

The idea behind this PR is that it's often useful when working in Typescript to be able to say that your service names will come from some pre-defined list, to (e.g.) prevent typos when declaring dependencies, or to get the compiler to error if you remove a service that another service still depends on. This PR makes that possible.

Usage:

  type LegalName = 'Service1' | 'Service2';

  const bottle = new Bottle<LegalName>();
  bottle.constant('Service1', 3); // Ok
  bottle.constant('Service3', 3); // Errors, no Service3
  bottle.service('Service1', class {}, 'Service2');
  bottle.service('Service2',  class {}, 'Service3'); // errors, no Service3.

Note that the type parameter for Bottle (LegalName in the example) will default to string if not provided, so this won't break any existing uses or require extra boilerplate for users who don't care about this safety.

@young-steveo young-steveo merged commit 2c7da6a into young-steveo:master Nov 1, 2018
@young-steveo
Copy link
Owner

Awesome, thanks for the contrib!

@young-steveo
Copy link
Owner

I'll release it on the next version.

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