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

时间:2020-09-17 18:24:26

标签: typescript-compiler-api

我想在.dt.s文件中合并类声明以生成更简洁的公共API。我被困在如何使它与泛型类型参数一起使用。假设我有:

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

export class B1 extends A1<string> {
}

理想情况下,我想将其转换为:

export class B1 {
  data?: string;
}

我可以获取A1的类型,然后复制其成员。但是如何获得使用A1而不是string的{​​{1}}的解析版本?

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

T

1 个答案:

答案 0 :(得分:0)

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

以下内容应获得string | undefined的已解析类型:

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