如果我正在使用导出类型为T
的模块,我该如何检查另一个标识符的类型是否与T
相匹配?
module-a
export type T = string;
index.ts
import { T } from 'module-a';
export const str: T = 'test';
compiler.ts
import ts from 'typescript';
const program = ts.createProgram(['./index.ts'], {});
const checker = program.getTypeChecker();
const source = program.getSourceFile('./index.ts');
const root = checker.getSymbolAtLocation(source);
root.exports.forEach((sym) => {
const test = checker.getApparentType(checker.getTypeOfSymbolAtLocation(sym, sym.valueDeclaration));
// Check here if `test` matches type `T` from `module-a`
});