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

Function should be assignable to (...args: any[]) => any #20007

Open
pelotom opened this issue Nov 14, 2017 · 4 comments
Open

Function should be assignable to (...args: any[]) => any #20007

pelotom opened this issue Nov 14, 2017 · 4 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@pelotom
Copy link

pelotom commented Nov 14, 2017

Obviously all functions have type Function. And likewise all functions can be given the type

type AnyFunc = (...args: any[]) => any;

AFAIK, there is nothing one can do with something of type Function that cannot be done with something of type AnyFunc and vice versa. However they are not equivalently assignable:

(f: AnyFunc): Function => f;
(f: Function): AnyFunc => f;
//                        ^
// Type 'Function' is not assignable to type 'AnyFunc'.
//   Type 'Function' provides no match for the signature '(...args: any[]): any'.

If I edit lib.d.ts to add a callable signature to Function:

interface Function {
  (...argArray: any[]): any;
  //...
}

it seems to "fix" this issue; is there any reason not to make this change for real?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 18, 2018

The original intention of Function is to not be callable. in other words, Function to function types should be like unknown to other types, but not callable. we have since relaxed this restriction giving Function a callable behavior in the compiler through special casing. We have talked about making this a --noImplicitAny error since, it is really unsafe to call Functions.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jul 18, 2018
@aleclarson
Copy link

aleclarson commented Dec 8, 2018

@mhegazy What's the benefit of such behavior, rather than treating Function as identical to (...args: any[]) => any? 🤔

I have a similar question about object and { [key: keyof any]: any } not being treated as identical.

@Pictor13
Copy link

Pictor13 commented Aug 29, 2022

I'm using Function in JSDoc, but when passing it as a parameter to a function expecting (...args: any[]) => any I'm getting the same problem.

Having CallableFunction helps with TypeScript, but I was wondering if there is a fix (or workaround) to avoid receiving the type error (from VSCode interpreter) when working with only JavaScript.
Apart for // @ts-ignore of course.

@aral
Copy link

aral commented Jul 29, 2024

For folks working in JavaScript:

/**
  @typedef {(...args: any[]) => any} JavaScriptFunction
*/

Then just use JavaScriptFunction where you expect a JavaScript function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants