javascript - 将 TypeScript 类型打印到控制台/文件中
问题描述
我有多种类型,它们构成了当前在我的服务器上使用的更大、更复杂的类型。是否可以将更大、更复杂的类型打印到控制台/文件中?
例子
type TypeA = {
prop1: string;
prop2: number;
}
type TypeB = Omit<TypeA, "prop2">;
console.logType(TypeB);
// {
// prop1: string;
// }
解决方案
为了提取类型签名,您需要使用编译器 API。假设你有一个文件:
// ./src/my-file.ts
type TypeA = {
prop1: string;
prop2: number;
}
type TypeB = Omit<TypeA, "prop2">;
从项目根目录外的脚本:
// ./type-printer.ts
import * as ts from "typescript";
function extractTypeSignature(filename: string, aliasName: string): string {
const program: ts.Program = ts.createProgram([ filename ], { emitDeclarationOnly: true });
const sourceFile: ts.SourceFile = program.getSourceFile(filename);
const typeChecker: ts.TypeChecker = program.getTypeChecker();
// Get the declaration node you're looking for by it's type name.
// This condition can be adjusted to your needs
const statement: ts.Statement | undefined = sourceFile.statements.find(
(s) => ts.isTypeAliasDeclaration(s) && s.name.text === aliasName
);
if (!statement) {
throw new Error(`Type: '${aliasName}' not found in file: '${filename}'`);
}
const type: ts.Type = typeChecker.getTypeAtLocation(statement);
const fields: string[] = [];
// Iterate over the `ts.Symbol`s representing Property Nodes of `ts.Type`
for (const prop of type.getProperties()) {
const name: string = prop.getName();
const propType: ts.Type = typeChecker.getTypeOfSymbolAtLocation(prop, statement);
const propTypeName: string = typeChecker.typeToString(propType);
fields.push(`${name}: ${propTypeName};`);
}
return `type ${aliasName} = {\n ${fields.join("\n ")}\n}`;
}
const typeBSignature = extractTypeSignature("./src/my-file.ts", "TypeB");
// write to file or console log
console.log(typeBSignature);
/*
type TypeB = {
prop1: string;
}
*/
我已经明确地注释了所有变量以显示类型的来源。即使它是一个小脚本,我还是建议使用 TypeScript 而不是 JavaScript 编写编译器脚本,并使用tsc file.ts && node file.js
或使用类似ts-node的东西执行,因为类型推断/类型保护在导航编译器 API 时非常有用。
推荐阅读
- nlp - 用于评估文本连贯性的注释数据集
- python - 如何使用 CSV 文件中包含的数据开发随机森林 Python 模型?
- python - NameError:名称“分数”未定义
- java - spring boot rest-api PUT 请求返回空值
- performance - J Meter - 移动应用程序录制 - SSL HandshakeException
- python - 如何检测树莓派板子型号?
- javascript - 使用图像数据处理后获取变换和缩放坐标
- r-markdown - 在 Bookdown (gitbook) 中通过“参考书目”更改“参考”
- paypal - PayPal Checkout 控制台错误:无法在“购物车”类型上查询“补充”字段
- r - 我如何告诉 R 包 Limma 在 read.idat() 中使用什么作为“目标”?