【发布时间】:2019-05-04 21:14:52
【问题描述】:
使用编译器 API,我需要从 ts.Signature 访问显式“this”参数的真实类型。
// Code being compiled
interface Fn1 {
(this: Foo): void;
}
const fn1: Fn1 = () => {};
interface Fn2<T> {
(this: T): void;
}
const fn2: Fn2<void> = () => {};
// Compiler API
function visitVariableDeclaration(node: ts.VariableDeclaration, checker: ts.TypeChecker) {
const type = checker.getTypeAtLocation(node.type);
const signatures = checker.getSignaturesOfType(type, ts.SignatureKind.Call);
const signature = signatures[0];
// How do I access type of 'this' on signature?
}
目前,我可以调用 getDeclaration() 并查看适用于 Fn1 的参数。但对于 Fn2,它不会将“T”解析为“无效”。使用调试器进行跟踪时,我可以看到签名有一个名为“thisParameter”的成员,它似乎有我需要的东西。但这并没有通过界面公开公开,所以我不能真正依赖它。有没有办法正确访问类型?
【问题讨论】:
标签: typescript typescript-compiler-api