如何将类型与导入的模块进行比较

时间:2020-01-08 21:47:24

标签: typescript-compiler-api

如果我正在使用导出类型为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`
});

0 个答案:

没有答案