【问题标题】:How do I determine the result type of a TypeScript.Expression object?如何确定 TypeScript.Expression 对象的结果类型?
【发布时间】:2015-10-18 07:18:30
【问题描述】:

使用 TypeScript 抽象语法树时,如何确定 TypeScript.Expression 对象的结果类型?

我正在使用 TSLint 并试图找到 setTimeout 的调用,这些调用不将 Function 类型的对象作为第一个参数传递。例如,在下面的代码中,我想知道调用了 setTimeout 并且第一个参数是一个函数。

// 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 来确定表达式的类型,但以下 API 都没有满足我的需求:

  • languageServices.getQuickInfoAtPosition
  • languageServices.getDefinitionAtPosition
  • languageServices.getTypeDefinitionAtPosition

有什么想法吗?

【问题讨论】:

    标签: typescript tslint


    【解决方案1】:

    语言服务本身不会公开这些信息。

    您可以使用类型检查器来执行此操作。一旦你有来自createProgramprogram 对象,请写:

            let typeChecker = program.getTypeChecker();
            let type = typeChecker.getTypeAtLocation(node);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 2016-09-06
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多