Skip to content

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

Closed
@RebeccaStevens

Description

@RebeccaStevens

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions