Closed
Description
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
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
Labels
No labels