Repository: TypeScript
Issue: Enum-specific interfaces enums could implement
Labels: Suggestion,Awaiting More Feedback

<!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.

Please help us by doing the following steps before logging an issue:
  * Search: https://github.com/Microsoft/TypeScript/search?type=Issues
  * Read the FAQ, especially the "Common Feature Requests" section: https://github.com/Microsoft/TypeScript/wiki/FAQ

-->

## Search Terms

<!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->
- enum interface typescript
- enum implements typescript

## Suggestion

<!-- A summary of what you'd like to see added or changed -->

Make interfaces work with enums.

### Enum interface

An interface describing what an enum should look like.

```ts
enum interface EnumInterface {
  Ok,
  NotOk
}
```


### Enum implements enum interface

Enum implements an `enum interface` by including in its values the fields described by the interface.

```ts
enum EnumName implements EnumInterface {
  Ok,
  NotOk,
  Other
}
```

This mechanism should also work for `const enums`. The same interface should be implementable by both a `const enum` and a regular `enum`.

An `enum interface` could require for the enum to have a specific value, although it is not necessary.

An `enum interface` which has strict requirements for both the names and values would throw an error where either the name is not present in the enum implementing the interface or if the name is present but the value is not the same as the one defined in the interface.

```ts
enum interface EnumInterface {
  Ok=0,
  NotOk=1
}
```

## Use Cases

<!--
What do you want to use this for?
What shortcomings exist with current approaches?
-->

Let say I'm creating an API. Each response I encode as a `const enum` result. Each of the response has some of the same results, (same possible errors) for example: 

Both a
- POST `/post` (creating a post) request and a 
- DELETE `/post` (deleting a post) request 

may trigger the same errors regarding lack of authentication. Here I demonstrate two `const enum`s describing the possible results of the two endpoints.

```ts
const enum CreatePost {
  OK,
  NO_AUTHORIZATION,
  OTHER_ERROR
}

const enum DeletePost {
  OK,
  NO_AUTHORIZATION,
  AUTHORIZED_BUT_NOT_AN_OWNER
}
```

Now, before the API handler *in a perfect world* one could create a method of signature:
```ts
declare function requireAuthentication(
   request: Request,
   handler: Handler,
   makeError: (result: RESULTS) => Response
): Handler;
```

Which would be fine, but `makeError` needs to be called from inside the `requireAuthentication` with a known error like, `NO_AUTHENTICATION`. So what is needed is:
- a status map object mapping from the kind of error that `requireAuthentication` middleware may trigger into the particular result of the particular endpoint (in this case either a `CreatePost` or `DeletePost` enum),
- another parameter for the middleware `requireAuthentication`.

```ts
interface AuthenticationErrorMap<Result extends RESULTS> {
   NO_AUTHORIZATION: Result
}

declare function requireAuthenticationReal<Result extends RESULTS>(
   request: Request,
   handler: Handler,
   makeError: (result: Result) => Response,
   statusCodeMap: AuthenticationErrorMap<Result>
): Handler;
```

Now with a few endpoints this is fine, but in production one can have a large number of endpoints for which a status map object has to be constructed manually for each endpoint:

```ts
const statusMapCreatePost: AuthenticationErrorMap<CreatePost> = {
   NO_AUTHORIZATION: CreatePost.NO_AUTHORIZATION
};
```

Now this is a simplified example, but in real life one could have lots of endpoints and lots of possible ways for things to fail.

## Examples

<!-- Show how this would be used and what the behavior would be -->

As for the solution, the perfect thing for this use-case would be to make **an enum interface**:
```
enum interface NeedsAuthorizationResults {
   NO_AUTHORIZATION
}
```

Which then could be implemented on all endpoints' result enums:
```
const enum CreatePost implements NeedsAuthorizationResults {
  OK,
  NO_AUTHORIZATION,
  OTHER_ERROR
}

const enum DeletePost implements NeedsAuthorizationResults {
  OK,
  NO_AUTHORIZATION,
  AUTHORIZED_BUT_NOT_AN_OWNER
}
```

Which would then stop us from having to create an object for each endpoint with the `AuthenticationErrorMap` type. Instead something like this could be written:

```ts
declare function requireAuthenticationProposal<Result extends NeedsAuthorizationResults>(
   request: Request,
   handler: Handler,
   makeError: (result: Result) => Response,
): Handler;
```

[Playground](https://www.typescriptlang.org/play/#code/MYewdgzgLgBApmArgWxgYQE5wIZTgBRGhgG8AoGGAeQGkAaCmAOSoH0BBAVQBUAJKgEoBJAFrtuQqkwaUqfAKIDWigYLIBfMmVCRYCFDAAicADZw8hYuVn1GLDj37CxEqTJhc+g0fMOsAQjysLNwcTKxUAOpMihpaUACeAA5wMALyAMqcADLcGTAAvOhYuAREsAA+RqbmZdAA3GSJKTC82GAAJmYYhTAAFFgAjgBcaXCDiHDQAJSFAHxjEEngEHCNZB1wwCbYWDAAZohgwFAAluAwQ4inWOyIUAAWCGfAuOdgfYyX45PQowI-KZQdwwB7tLpwDCjNqdbog5DYADWcHkGAwICh-SwEEQJig-0yOTyswKCwBSxWcDI02h4O661OYDwGH22GAqTuj2ep1eZ3AqPRGAAstgkgAecm4vQADzwnQgaUJuQyC2slHsniconEkiY-ymUriGy2Oz2h2OfLA3wmNzgnKeTJ5b3AAOwJglBrx8FlCA6CqYcDgfvtGNOAC9nWBJXiIHNPpRrb98WMJkCQWDYZDaZmMPCkSi0RjRgNPcno1ASWSpstIHAQdBcIgIGgQJsRUlRvbubz3gKMe2PTi8XNqdmIRh1jpiA2oE325gcBZyp37g6XpG+8LRWKF6VLFAFkU1cw2JrvC5daNd0voAA6DWOc86qQadb6VCM5ms9nMQPB1ehhGlrlgqx4Pl4zjPkwRpTnoSCoNedSwKcyBJGYyDPP6f4QCGGDhpGpCMLQ7jgVqF5uERChKCoaiaNoKxwQYxhmDeyGoehmG-kGOEAXhQHvIRNgkaej6Qa40iMGezi+AEQQhGEETRLEdGbNsuypOaJwCVctpdo6PbgPg6LLBAbqDlK3pyn6XH-o8gGRiBcZfFcQL6qm0DpnSWatF5uZfAiyKbsW2JSvqQ4VvMiw1qsDA0j5Ob1EAA).


## Checklist

My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).