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

Aliasing enums #3832

Closed
AlicanC opened this issue Jul 12, 2015 · 6 comments
Closed

Aliasing enums #3832

AlicanC opened this issue Jul 12, 2015 · 6 comments
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@AlicanC
Copy link

AlicanC commented Jul 12, 2015

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

export type HTTPParameters = { [key: string]: string };
export type HTTPHeaders = { [key: string]: string };
export enum HTTPMethod { Get, Head, Post, Put, ... };

api.ts (->Arrows<- point out where the errors are.)

import { HTTPParameters, HTTPHeaders, HTTPMethod } from './http.ts';

export type ApiParameters = HTTPParameters;
export type ApiHeaders = HTTPHeaders;

/* Try one */
export type ApiMethod = HTTPMethod;

export class Foo {
  constructor(public method: ApiRequestMethod = ->ApiRequestMethod<-.Get) {}
  // Cannot find name 'ApiRequestMethod'
}

/* Try two */
export var ApiMethod = HTTPMethod;

export class Foo {
  constructor(public method: ->ApiRequestMethod<- = ApiRequestMethod.Get) {}
  // Cannot find name 'ApiRequestMethod'
}

/* Try three */
export enum ApiRequestMethod ->=<- HTTPRequestMethod;
// Expected '{'

/* Try four */
export enum ApiRequestMethod ->HTTPRequestMethod<-;
// Expected '{'

The actual working solution looks like this:

import { SomeEnum as MyEnum } from './http.ts';

export { MyEnum };

export class Foo {
  constructor(public method: MyEnum = MyEnum.Get) {}
}

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.

@RyanCavanaugh
Copy link
Member

What would your ideal case look like?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 13, 2015

why not just use import to alias the enum:

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'
}

@AlicanC
Copy link
Author

AlicanC commented Jul 13, 2015

@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 enum MyEnum = SomeEnum; still looks better though.

@RyanCavanaugh
Copy link
Member

I think the easier fix would be to allow unqualified imports, e.g. export import MyEnum = SomeEnum. It's kind of odd that you have to wrap something in a namespace to get a new name for it.

@AlicanC
Copy link
Author

AlicanC commented Jul 14, 2015

That's great.

I was aiming for enum MyEnum = SomeEnum without involving any import/export shenanigans because the next thing I was going to suggest was this:

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);

    // ...

  }
}

@danquirk danquirk added the Suggestion An idea for TypeScript label Jul 14, 2015
@mhegazy mhegazy added the In Discussion Not yet reached consensus label Dec 9, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2015

looks like a duplicate of #1166

@mhegazy mhegazy closed this as completed Dec 9, 2015
@mhegazy mhegazy added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus labels Dec 9, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants