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

isFunction() or isCallable() type queries? #1458

Open
elichten94 opened this issue Sep 28, 2023 · 0 comments
Open

isFunction() or isCallable() type queries? #1458

elichten94 opened this issue Sep 28, 2023 · 0 comments

Comments

@elichten94
Copy link

elichten94 commented Sep 28, 2023

It would be useful to have a utility method that can query if a Type<ts.Type> is callable.

The existing queries that can be performed on a Type<ts.Type>, such as isObject(), isNumber(), isLiteral(), etc. seem straightforward, though I don't see anything to check if a type is a method, function, callable etc.

A callable type could be far up an inheritance chain from the raw Function or (...args: any[]) => any types, so I assume this could be difficult to check. I also noticed that isObject() returns true for callables (since they're considered first class objects in js/ts anyway).

I've considered the following alternative, though it doesn't seem to work as expected, might have some risky edgecases, and and looks expensive to run.

export const isCallableType = (typeNode: Node): boolean => {
  const isCallable =
    Node.isFunctionDeclaration(typeNode) ||
    Node.isFunctionExpression(typeNode) ||
    Node.isCallSignatureDeclaration(typeNode) ||
    Node.isFunctionLikeDeclaration(typeNode) ||
    Node.isFunctionTypeNode(typeNode) ||
    Node.isArrowFunction(typeNode) ||
  if (isCallable) {
    return true;
  } else {
    if (typeNode.getChildren().length === 0) {
      return false;
    } else {
      return typeNode.getChildren().some((child) => isCallableType(child));
    }
  }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant