通过TypeScript API或ts morph从模块获取所有可能的导出
下面的代码是使用通过TypeScript API或ts morph从模块获取所有可能的导出,typescript,typescript-compiler-api,Typescript,Typescript Compiler Api,下面的代码是使用ts morph编写的,它是typescript的包装器 // Short version of node_modules/@types/node/path.d.ts declare module 'path' { namespace path { interface ParsedPath {} interface FormatInputPathObject {} interface PlatformPath {
ts morph
编写的,它是typescript的包装器
// Short version of node_modules/@types/node/path.d.ts
declare module 'path' {
namespace path {
interface ParsedPath {}
interface FormatInputPathObject {}
interface PlatformPath {
normalize(p: string): string;
join(...paths: string[]): string;
// other...
}
}
const path: path.PlatformPath;
export = path;
}
这里我得到了三个导出符号,但我们知道normalize
、join
以及path
模块中应该存在的其他函数
const sourceFile = project.createSourceFile('source.ts', sourceFileContent);
const importDeclarations = sourceFile.getImportDeclarations();
// loop for importDeclarations
let symbol = importDeclaration.getImportClause().getSymbol();
symbol = typeChecker.getAliasedSymbol(symbol);
const exportSymbols = typeChecker.getExportsOfModule(symbol);
// {"ParsedPath" => SymbolObject, "FormatInputPathObject" => SymbolObject, "PlatformPath" => SymbolObject}
所以我一直在获取这些函数(normalize
,join
,等等),你能帮我吗
通过使用
ts morph
或裸类型脚本API从模块获取所有可能的导出?我不知道如何使用符号来实现这一点,但可以通过检查其类型来获取路径
对象上的属性:
import { normalize, join } from 'path'; // no errors
产出:
const defaultImport = importDeclaration.getDefaultImportOrThrow();
const defaultImportType = defaultImport.getType();
for (const property of defaultImportType.getProperties()) {
console.log(property.getName());
}
const defaultImport = importDeclaration.getDefaultImportOrThrow();
const defaultImportType = defaultImport.getType();
for (const property of defaultImportType.getProperties()) {
console.log(property.getName());
}
normalize
join
resolve
isAbsolute
relative
dirname
basename
extname
sep
delimiter
parse
format
toNamespacedPath
posix
win32