如何确定 TypeScript.Expression 对象的结果类型? [英] How do I determine the result type of a TypeScript.Expression object?
问题描述
使用 TypeScript 抽象语法树时,如何确定 TypeScript.Expression 对象的结果类型?
When working with the TypeScript abstract syntax tree, how do I determine the result type of a TypeScript.Expression object?
我正在使用 TSLint 并尝试查找未将 Function 类型的对象作为第一个参数传递的 setTimeout 调用.例如,在下面的代码中,我想知道调用了 setTimeout 并且第一个参数是一个函数.
I am using TSLint and attempting to find invocations of setTimeout that do not pass an object of type Function as the first parameter. For example, in the following code, I want to know that setTimeout was invoked and that the first parameter is a function.
// function that produces a function
var createFunction : () => (() => void) = () => {};
// result of createFunction() should be of type function
setTimeout(createFunction());
AST 排列如下:
- setTimeout -> TypeScript.CallExpression
- createFunction() -> TypeScript.Expression
我曾尝试使用 LanguageService 来确定 Expression 的类型,但以下 API 均无法满足我的需求:
I have tried to use the LanguageService to determine the type of the Expression, but none of the following APIs give me what I need:
- languageServices.getQuickInfoAtPosition
- languageServices.getDefinitionAtPosition
- languageServices.getTypeDefinitionAtPosition
有什么想法吗?
推荐答案
语言服务本身不会公开这些信息.
The language service itself doesn't expose that information.
您可以使用类型检查器来执行此操作.从 createProgram
获得 program
对象后,请编写:
You can use the type checker to do this. Once you have a program
object from createProgram
, write:
let typeChecker = program.getTypeChecker();
let type = typeChecker.getTypeAtLocation(node);
这篇关于如何确定 TypeScript.Expression 对象的结果类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!