You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to add a "implements " option to functions.
Use Cases
Sometimes anonymous function or even named functions are in use in a further callback.
This is very prevalent in node.js.
Currently typescript does allow you to check variable which are functions, that they fit to a type.
It also allows you to check that a callback that you pass has a right signature.
But sometimes, whenever you write a module , you expose functions which will be used with different module which aren't available to you, (whether because the exposed function must be generic, or it hasn't been written or for a different reason).
The idiomatic way of ts to declare a function is something like this:
function my_pretty_function():void
{
}
Whereas had we wanted to enforce typing we needed to:
let my_pretty_function :my_beautiful_function_type=function():void
{
}
what I suggest is to add an implement keyword:
function implements my_beautiful_function_type my_pretty_function() :void
{
}
Examples
type num_type=()=>number;
function implements num_type my_pretty_function() :void
{
}
ts should print: signature mismatch, function my_pretty_function doesn't match num_type type.
Another interesting feature which might be implemented using this semantic addition is IDE auto complete.
Say you type: function implements num_type square_root
IDEs will be able to auto-complete or suggest:
function implements num_type square_root():number
{
}
Checklist
My suggestion meets these guidelines:
[ V] This wouldn't be a breaking change in existing TypeScript/JavaScript code
[V ] This wouldn't change the runtime behavior of existing JavaScript code
[V ] This could be implemented without emitting different JS based on the types of the expressions
[ V] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Search Terms
function, interface, function-type
Suggestion
I would like to add a "implements " option to functions.
Use Cases
Sometimes anonymous function or even named functions are in use in a further callback.
This is very prevalent in node.js.
Currently typescript does allow you to check variable which are functions, that they fit to a type.
It also allows you to check that a callback that you pass has a right signature.
But sometimes, whenever you write a module , you expose functions which will be used with different module which aren't available to you, (whether because the exposed function must be generic, or it hasn't been written or for a different reason).
The idiomatic way of ts to declare a function is something like this:
Whereas had we wanted to enforce typing we needed to:
let my_pretty_function :my_beautiful_function_type=function():void
{
}
what I suggest is to add an implement keyword:
Examples
ts should print: signature mismatch, function my_pretty_function doesn't match num_type type.
Another interesting feature which might be implemented using this semantic addition is IDE auto complete.
Say you type:
function implements num_type square_root
IDEs will be able to auto-complete or suggest:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: