microsoft / microsoft/TypeScript

Allow marking functions in interfaces as callback / non-callable

Đang mở
#30,169 8 bình luận 0 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

callable function, non-callable function, callback function type, callback type modifier

Suggestion

A keyword and some language support to identify functions which are not callable (they are only assignable callbacks).

Use Cases

Currently, it is possible to call functions which aren't intended to be called, for example:

const btn = document.createElement('button')
btn.onclick = evt => {} // Correct use
btn.onclick(new MouseEvent('')) // Incorrect use, but no error

It should be possible to declare that onclick is only a callback, and thus you must not call it directly. A keyword such as callback, noncallable or nocall could be used for this purpose, in a spirit similar to the readonly modifier. For example:

interface HTMLButtonElement {
    callback onclick: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
}

Similar capabilities to that of readonly should be implemented around it, such as being able to add and remove the modifier through mapped types:

type FullyCallable<T> = {
    -callback [P in keyof T]: T[P];
}

Which would set the stage for later usage in conditional types. For example, this is a way one can currently use to extract writable properties from a type:

type Not<T extends boolean> = T extends true ? false : true

type Equals<X, Y> =
    (<T> () => T extends X ? 1 : 2) extends
        (<T> () => T extends Y ? 1 : 2)
            ? true
            : false

type IsReadonly<O extends Record<any, any>, P extends keyof O> =
    Not<Equals<{[_ in P]: O[P]}, {-readonly [_ in P]: O[P]}>>

type WritableProperties<O extends Record<any, any>> = {
    [P in keyof O]: IsReadonly<O, P> extends true
        ? never
        : P
} extends {[_ in keyof O]: infer U}
    ? U
    : never

type Writable<O extends Record<any, any>> = Pick<O, WritableProperties<O>>

type T = Writable<{
    n: number,
    readonly b: boolean,
    o: {
        readonly s: string // Recursion is possible... But even crazier.
    }
}> // { n: number; o: { readonly s: string; }; }

It would be convenient to be able to do a similar thing with callbacks, identifying them and removing them at will while defining new types.

(A different topic altogether, but I hope declaring types like those above becomes more straightforward in the future, e.g. by having a direct way to identify readonly properties, or by being able to use logical operators as part of conditional types. Feel free to pick up on these ideas and start separate issues... Or I can do it myself if you ask me to. Edit I finally did: #31581, #31579)

Example

The task I have at hand at the moment is to define the following button-spawning function (more generally, I would like to do this for any Element):

type ButtonProperties<T> = any

function button(properties: ButtonProperties<HTMLButtonElement>) {
    const btn = document.createElement('button')
    // Assign all `properties` to `btn`...
    return btn
}

But I would like to define ButtonProperties<T> in such a way that it removed from HTMLButtonElement properties meeting certain criteria, namely:

  • Are readonly. (Managed.)
  • Are index signatures. (Managed.)
  • Are callbacks. (Sort-of managed, with the assumption that all callbacks defined in Elements take as a first argument something extending Event, which is not necessarily fully accurate and it does not scale to user-defined types.)

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

Không có tệp, bài kiểm thử hoặc điểm vào nào được nêu tên. Hãy bắt đầu từ các ví dụ về interface, callback, mapped-type và readonly trong issue; để được xem là hoàn tất, cần định nghĩa một Modifier không thể gọi, hỗ trợ thêm và xoá Modifier đó trong mapped types, và phát sinh lỗi đối với các lệnh gọi trực tiếp.

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.