microsoft / microsoft/TypeScript

Enum-specific interfaces enums could implement

Đang mở
#39,773 1 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

Search Terms

  • enum interface typescript
  • enum implements typescript

Suggestion

Make interfaces work with enums.

Enum interface

An interface describing what an enum should look like.

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.

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.

enum interface EnumInterface {
  Ok=0,
  NotOk=1
}

Use Cases

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 enums describing the possible results of the two endpoints.

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:

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.
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:

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

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:

declare function requireAuthenticationProposal<Result extends NeedsAuthorizationResults>(
   request: Request,
   handler: Handler,
   makeError: (result: Result) => Response,
): Handler;

Playground.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách xem xét đề xuất enum-interface, ví dụ TypeScript Playground của đề xuất này và các TypeScript design goals được liên kết. Một triển khai hoàn chỉnh sẽ cần hỗ trợ các enum thông thường và const, áp dụng các tên và giá trị member được đề xuất, đồng thời xác thực trường hợp sử dụng generic được minh họa.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.