Skip to content

Allow function declarations to be typed via a type alias #41411

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

Closed
5 tasks done
RebeccaStevens opened this issue Nov 5, 2020 · 2 comments
Closed
5 tasks done

Allow function declarations to be typed via a type alias #41411

RebeccaStevens opened this issue Nov 5, 2020 · 2 comments

Comments

@RebeccaStevens
Copy link

Search Terms

"function declaration" "type alias"

Suggestion

Currently functions can only typed via a type alias when they are an expression, function declarations can't be typed this way.
Could typing function declarations via type alias be supported?

Use Cases

Declaring a function's type via a type alias.

Examples

In a declaration file

type F = <T>(param: T) => string;

// New Syntax
declare function foo: F;

declare namespace foo { /* ... */ }

declare const foo: F; doesn't work in the above example as it results in "Cannot redeclare block-scoped variable 'foo'. ts(2451)"

In a standard TypeScript file

type F = <T>(param: T) => string;


// New Syntax: Casting
function bar1(param) { /* ... */ } as F
<F>function bar2(param) { /* ... */ }

// New Syntax: Declaring
function bar3: F (param) { /* ... */ }

Playground

https://www.typescriptlang.org/play?#code/C4TwDgpgBAYlC8UA8AVAfACjAQwE7YFsAuKFASgTSgDcB7ASwBMBuAKFYGNaA7AZ2CjAAFvV4B1WrgDWvEnERY8hCvCoBvKAHoAVFAB0BqNs1QAvm1aMIHADZ5oAMwCu3DsHo8oD2rTkWrtvZQ3IQQvDgcjj5QGjr6hsZm7M6u7p4ARngAjIr4BBSxugZ6RiamUNi8sOxIMGgpbh7cUJm4AEy5yjFaRQllyS6NGXgAzHJQnfndccWlZkA

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.
@MartinJohns
Copy link
Contributor

The second part (in a standard TypeScript file) is a duplicate of #40378.

@RebeccaStevens
Copy link
Author

I'll close this one in favor of that one. I comment the relevant parts of this one there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants