Skip to content
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

add "implements" keyword for functions #41447

Closed
exx8 opened this issue Nov 8, 2020 · 2 comments
Closed

add "implements" keyword for functions #41447

exx8 opened this issue Nov 8, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@exx8
Copy link

exx8 commented Nov 8, 2020

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:

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.)
  • [V ] This feature would agree with the rest of TypeScript's Design Goals.
@exx8 exx8 changed the title add "implements" keyword for functions. add "implements" keyword for functions Nov 8, 2020
@MartinJohns
Copy link
Contributor

Duplicate of #40378.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 9, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants