TypeChecker API:如何查找函数的推断类型参数?

时间:2018-02-20 13:33:31

标签: typescript typescript-compiler-api

有没有办法,给定带有推断类型参数的CallExpression,找到那些类型参数是什么?

示例代码:

class SomeClass {
    public someMethod<T>(arg: T): void { }
}

// What is the inferred type of T in this call?
someClass.someMethod(7);

找到在代码中明确指定的类型参数很容易,但我无法弄清楚如何找到推断的内容。

function inferTypeArguments(node: ts.CallExpression, typeChecker: ts.TypeChecker) {
    node.typeArguments; // is empty
    const signature = typeChecker.getResolvedSignature(node);
    signature['typeArguments']; // is also empty

    // This returns "<number>(arg: number): void"
    // so I know that the typeChecker has the right information,
    // but I would really like a ts.Type[]
    typeChecker.signatureToString(signature, node, ts.TypeFormatFlags.WriteTypeArgumentsOfSignature)
}

2 个答案:

答案 0 :(得分:1)

如果有人找到更好的方法,我会打开这个问题,但我确实通过使用&#34; Signature&#34;的内部成员来实现这一目标。

type TypeMapper = (t: ts.TypeParameter) => ts.Type;
function inferTypeArguments(node: ts.CallExpression, typeChecker: ts.TypeChecker): ts.Type[] {
  const signature = typeChecker.getResolvedSignature(node);
  const targetParams: ts.TypeParameter[] = signature['target'] && signature['target'].typeParameters;
  if (!targetParams) {
    return [];
  }
  const mapper: TypeMapper = signature['mapper'];
  return mapper
    ? targetParams.map(p => mapper(p))
    : targetParams;
}

答案 1 :(得分:0)

我在转译器中使用Simon的代码已经有一段时间了...然后3.9出现并破坏了它。我已经做了初步的尝试,以使其再次工作。不幸的是,映射器是打字稿的“内部”问题,因此将来可能会再次改变

/* @internal - from typescript 3.9 codebase*/
const enum TypeMapKind {
  Simple,
  Array,
  Function,
  Composite,
  Merged,
}

/* @internal - from typescript 3.9 codebase*/
type TypeMapper =
  | { kind: TypeMapKind.Simple, source: ts.Type, target: ts.Type }
  | { kind: TypeMapKind.Array, sources: readonly ts.Type[], targets: readonly ts.Type[] | undefined }
  | { kind: TypeMapKind.Function, func: (t: ts.Type) => ts.Type }
  | { kind: TypeMapKind.Composite | TypeMapKind.Merged, mapper1: TypeMapper, mapper2: TypeMapper };

/* basic application of the mapper - recursive for composite.*/ 
function typeMapper(mapper:TypeMapper, source: ts.Type): ts.Type {
  switch(mapper.kind){
    case TypeMapKind.Simple: 
      return mapper.target;
    case TypeMapKind.Array:
      throw Error("not implemented");
    case TypeMapKind.Function: 
      return mapper.func(source);      
    case TypeMapKind.Composite: 
    case TypeMapKind.Merged:
      return typeMapper(mapper.mapper2, source);
  }
}

function inferTypeArguments(node: ts.CallExpression, typeChecker: ts.TypeChecker): ts.Type[] {
  const signature:ts.Signature = typeChecker.getResolvedSignature(node);
  const targetParams: ts.TypeParameter[] = signature['target'] && signature['target'].typeParameters;

  if (!targetParams) {
    return [];
  }

  if(signature['mapper'] == undefined)   
    return targetParams;   

  //typescript <= 3.8
  if(typeof signature['mapper'] == "function") 
    return targetParams.map(p=>signature['mapper'](p));
  //typescript >= 3.9.... 
  return targetParams.map(p=> typeMapper(signature['mapper'] as TypeMapper, p));
}