dsherret / dsherret/ts-morph

isFunction() or isCallable() type queries?

Open
#1,458 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

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

The existing queries that can be performed on a `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.
```typescript
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));
}
}
};
```

Contributor guide

Open the contributing guide

Research direction

No file or test is named. Start by reviewing the existing Type queries such as isObject(), isNumber(), and isLiteral(), then define how callable types inherited from Function or function signatures should be recognized; done means a consistent callable query with behavior validated for those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.