typescript-compiler-api - Typescript编译器,如何按名称获取导出的符号?
问题描述
按名称获取导出符号的最佳方法是什么?
波纹管是功能代码。但是,它似乎有点脆弱,因为我无法在不忽略类型系统的情况下从“sourceFile”获取“符号”。
const sourceFile = tsprogram.getSourceFile('foo_file.ts');
const fileSymbol = (sourceFile as any).symbol as ts.Symbol; // anything better her?
const export = fileSymbol.exports.get('FooComponent');
解决方案
使用类型检查器:
const fileSymbol = tsprogram.getTypeChecker().getSymbolAtLocation(sourceFile);
const fooComponentSymbol = fileSymbol?.exports.get('FooComponent');
请注意,fileSymbol
当没有文件导出时,它将是未定义的。
推荐阅读
- python - 沿其最后一维对数组进行排序,并撤消排序
- html - 如何创建跨越 3 个垂直 div 元素的徽标图像
- country-codes - 国家代码 API
- javascript - React 浅拷贝仍然触发重新渲染?
- java - Spring-Webflux:使用两个 Mono 响应
- powerbi - 固定日期和计算日期之间的 Power BI 测量
- google-maps - Blazor 3 中的谷歌地图停止连接到我的组件
- sql - 使用别名和嵌套选择更新不起作用
- python - 从 Snowflake 中的 Temp 表中读取数据到 Jupyter Notebook
- algorithm - 如何检查二维数组中的每个元素是否连接在一起