Typescript compiler api TypeScript编译器API:如何使用已解析的类型参数获取类型?

Typescript compiler api TypeScript编译器API:如何使用已解析的类型参数获取类型?,typescript-compiler-api,Typescript Compiler Api,我想在.dt.s文件中合并类声明,以生成更干净的公共API。我一直在研究如何使用泛型类型参数来实现这一点。假设我有: class A1<T> { // Non-exported class I want to hide data?: T; } export class B1 extends A1<string> { } 我可以获取A1的类型,然后复制其成员。但是如何获得使用string而不是T的A1解析版本呢 作为参考,这是我当前的代码: for (const h

我想在.dt.s文件中合并类声明,以生成更干净的公共API。我一直在研究如何使用泛型类型参数来实现这一点。假设我有:

class A1<T> { // Non-exported class I want to hide
  data?: T;
}

export class B1 extends A1<string> {
}
我可以获取
A1
的类型,然后复制其成员。但是如何获得使用
string
而不是
T
A1
解析版本呢

作为参考,这是我当前的代码:

for (const heritageClause of node.heritageClauses) {
  for (const type of heritageClause.types) {
    if (isExported(type.modifiers)) {
      exportedTypes.push(type);
    } else {
      const privateType = typeChecker.getTypeAtLocation(type);
      if (privateType?.symbol?.members) {
        privateType.symbol.members.forEach((definition, memberName) => {
          if (!currentMembers || !currentMembers.has(memberName)) {
            additionalMembers.push(...definition.declarations);
           }
         }
      });
    }
  }
}

我相信您正在寻找的方法是
TypeChecker#getTypeOfSymbolAtLocation(symbol,node)

以下内容应获得
字符串|未定义的解析类型

// by the way, recommend renaming `type` to `typeNode` to avoid confusion
typeChecker.getTypeOfSymbolAtLocation(privateType.getProperties()[0], type);
// by the way, recommend renaming `type` to `typeNode` to avoid confusion
typeChecker.getTypeOfSymbolAtLocation(privateType.getProperties()[0], type);