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

Type Cast or Assertions on the FunctionDeclaration #40378

Open
5 tasks done
Jack-Works opened this issue Sep 4, 2020 · 3 comments
Open
5 tasks done

Type Cast or Assertions on the FunctionDeclaration #40378

Jack-Works opened this issue Sep 4, 2020 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Jack-Works
Copy link
Contributor

Jack-Works commented Sep 4, 2020

Search Terms

function declaration type cast assertions

Suggestion

A new syntax to cast or assert function declaration as a type T.

function MyFunction() {} as React.ComponentType
// same as
const MyFunction = (function MyFunction() {}) as React.ComponentType

function F2() {} asserts FunctionWithAdditionalProperty
// same as
function F2() {}
function _assert_(f: FunctionWithAdditionalProperty) {}
_assert_(F2)

Use Cases

In some cases, we want to type-check a function to obey a type constraint but it is not easy to do on the FunctionDeclaration statement.

Examples

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.
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Sep 4, 2020
@RebeccaStevens
Copy link

My proposed syntax for this is as follows:

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

// New syntax for casting:
function foo1(param) { /* ... */ } as F
<F>function foo2(param) { /* ... */ }

// New syntax for declarations:
function foo3: F (param) { /* ... */ }
declare function foo4: F;

Note: declare function foo4: F differs from declare const foo4: F as the former should work in the following case while the later fails.

declare const foo4: F;	// <-- Cannot redeclare block-scoped variable 'foo4'. ts(2451)
declare namespace foo4 { /* ... */ }

@jekh
Copy link

jekh commented Jan 13, 2022

Is this a duplicate of #22063? I'm wondering if we can consolidate these these issues to focus attention on them. I would love to see the ability to type function declarations, no matter what syntax is ultimately chosen.

@Jack-Works
Copy link
Contributor Author

Is this a duplicate of #22063? I'm wondering if we can consolidate these these issues to focus attention on them. I would love to see the ability to type function declarations, no matter what syntax is ultimately chosen.

Yes I guess?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants