-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Aliasing enums #3832
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
Comments
What would your ideal case look like? |
why not just use import * as myModule from "module2";
// alias declaration
export import myEnum = myModule.E;
export class Foo {
constructor(public method: myEnum = myEnum.GET) { }
// Cannot find name 'ApiRequestMethod'
} |
@RyanCavanaugh, my ideal case would look like this: enum HttpMethods { Get, Post };
// I think it's actually a shorthand for something like this:
type HttpMethods = any;
var HttpMethods = {
Get: <HttpMethods>0,
Post: <HttpMethods>1,
0: 'Get',
1: 'Post'
};
// Current situation, if my observations are correct
type ApiMethods = HttpMethods; // Only copies the type part of HttpMethods, can't do "ApiMethods.Get"
var ApiMethods = HttpMethods; // Only copies the value part of HttpMethods, can't do "var apiMethod: ApiMethods;"
/* Proposal */
// Copies both characteristics of HttpMethods, can do both above
enum ApiMethods = HttpMethods;
/*
I think making the following available would cause confusion so they can cause errors
*/
enum ApiMethods = { Get, Post }; // "Get is not defined." because it's considered an object initializer shorthand
var Get = 0;
var Post = 1
enum ApiMethods = { Get, Post }; // "{ Get: number, Post: number } is not an enum." because we can only provide enums
enum ApiMethods = { Get: 0, Post: 1 }; // "{ Get: number, Post: number } is not an enum."
enum ApiMethods = "yippee ki-yay"; // "string is not an enum." @mhegazy, yes, that's another solution. Being able to do |
I think the easier fix would be to allow unqualified imports, e.g. |
That's great. I was aiming for enum HttpMethodsWithoutBody { Get };
enum HttpMethodsWithBody { Post: 1 };
// The next thing, could be called "Enum Extending"
enum HttpMethods = HttpMethodsWithoutBody + HttpMethodsWithBody;
// Usage example
class HTTPRequester {
sendRequest(method: HttpMethodsWithoutBody);
sendRequest(method: HttpMethodsWithBody, body: string);
sendRequest(method: HttpMethods, body?: string) {
// Should we expect a body?
var expectBody = (HttpMethodsWithBody[method] !== undefined);
// ...
}
} |
looks like a duplicate of #1166 |
I have a few files for making HTTP requests. I want to extend those to allow easier access to our API. Here is some sample code:
http.ts
api.ts (->Arrows<- point out where the errors are.)
The actual working solution looks like this:
I might be missing something here, but if this is really the only solution, then it's pretty ugly and it would be nice to have something better.
The text was updated successfully, but these errors were encountered: