Skip to content

Allow function declarations to be typed #39419

@hdodov

Description

@hdodov

Search Terms

set function declaration type

Suggestion

To define a function type, we currently have to use function expressions:

type Fun<T> = (value: T) => T

export let foo: Fun<number> = function (value) {
	return value
}

My suggestion is to make that possible for function declarations as well:

export function foo: Fun<number> (value) {
	return value
}

Use Cases

As a superset of JavaScript, TypeScript shouldn't enforce us to change the actual runtime logic in order to apply types. In this case, you're forced to use a function expression when a function declaration is fine.

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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions