-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Allow marking functions in interfaces as callback / non-callable #30169
Comments
This is just |
Hmmm... Indeed! I hadn't thought of it this way, but you are right. And I see that there is already an issue for that - thumbs up! #21759 |
Watch out for one thing, though! You may want to "read" a callback property, and yet not call it, e.g. if (!btn.onclick) // read
btn.onclick = ... // write What are your thoughts on that? It is nevertheless very true that my proposal overlaps a lot with |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Please @RyanCavanaugh, have a look at my last comment. |
I think the issue being described is best solved by There are a lot of functions in the world that are only supposed to be called by certain parts of a program and defining a new (somewhat paradoxical) primitive in the type system to describe that "It just happens that all valid callers of this function are outside the text of my program" is too weird. If you have your own API that you want to type this way, you can always put an impossible parameter at the end so that you don't accidentally invoke it, e.g. type GuardedCallback = (n: number, s: string, NOT_FOR_YOU_TO_CALL: "I AM ILLEGALLY CALLING THIS FUNCTION") => void; |
Just a couple of comments:
|
Please @RyanCavanaugh, reopen until completing the discussion. |
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:
It should be possible to declare that
onclick
is only a callback, and thus you must not call it directly. A keyword such ascallback
,noncallable
ornocall
could be used for this purpose, in a spirit similar to thereadonly
modifier. For example:Similar capabilities to that of
readonly
should be implemented around it, such as being able to add and remove the modifier through mapped types: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:
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
):But I would like to define
ButtonProperties<T>
in such a way that it removed fromHTMLButtonElement
properties meeting certain criteria, namely:Element
s take as a first argument something extendingEvent
, which is not necessarily fully accurate and it does not scale to user-defined types.)Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: