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

Use const enums for BindingScope, BindingType, TargetType #706

Closed
JoshuaKGoldberg opened this issue Dec 9, 2017 · 6 comments
Closed

Use const enums for BindingScope, BindingType, TargetType #706

JoshuaKGoldberg opened this issue Dec 9, 2017 · 6 comments

Comments

@JoshuaKGoldberg
Copy link
Contributor

Now that TypeScript supports string values for enums, it'd be very convenient to switch from this:

type BindingScope = "Singleton" | "Transient" | "Request";
type BindingType = "ConstantValue" | "Constructor" | "DynamicValue" | "Factory" | "Function" | "Instance" | "Invalid" | "Provider";
type TargetType = "ConstructorArgument" | "ClassProperty" | "Variable";    

...to this:

const enum BindingScope {
    Singleton = "Singleton",
    Transient = "Transient",
    Request = "Request",
}

const enum BindingType {
    ConstantValue = "ConstantValue",
    Constructor = "Constructor",
    DynamicValue = "DynamicValue",
    Factory = "Factory",
    Function = "Function",
    Instance = "Instance",
    Invalid = "Invalid",
    Provider = "Provider",
}

const enum TargetType {
    ConstructorArgument = "ConstructorArgument",
    ClassProperty = "ClassProperty",
    Variable = "Variable",
}

That way, instead of hardcoding string literals:

const container = new Container({
    defaultScope: "Singleton",
});

...we can use slightly-easier-to-get-intellisense-for enum members:

const container = new Container({
    defaultScope: BindingScope.Singleton,
});
@remojansen
Copy link
Member

This change looks good to me. Are u planning to send a PR?

@JoshuaKGoldberg
Copy link
Contributor Author

Yes :)

@JoshuaKGoldberg
Copy link
Contributor Author

You know, this actually is a breaking change. You would no longer be able to use sring literals as many likely do. I'll hold off the change.

microsoft/TypeScript#17690
microsoft/TypeScript#3192 (comment)

Steps to make the change locally are:

  • Delete literal_types.ts
  • Perform the OP's suggested replacement in interfaces.ts
  • Replace usages of the enum types exported from that file with the newly exported enums in interfaces.ts

@JoshuaKGoldberg
Copy link
Contributor Author

microsoft/TypeScript#17690 (comment)

Will just use a const object.

@remojansen
Copy link
Member

remojansen commented Dec 13, 2017

@remojansen remojansen mentioned this issue Dec 15, 2017
10 tasks
@remojansen
Copy link
Member

Closing this issue by #718

You can now do:

import { BindingScopeEnum, Container  } from "inversify";

const container = new Container({
    defaultScope: BindingScopeEnum.Singleton,
});

This feature is available in 4.7.0 🎉

This issue was closed.
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

No branches or pull requests

2 participants