typescript - typescript compiler api - 如何检测属性类型是枚举还是对象
问题描述
我必须知道每个属性声明节点,如果是枚举或另一个对象。
所以我得到了类型参考:
const typeReferance = (arrayType as any).typeName
但我不知道如何检测是枚举还是对象。
楷模:
enum Color { Red, Green, Blue }
class SomeObject {
string: string;
}
class Model {
color:Color;
}
访客:
if (node.kind === ts.SyntaxKind.PropertyDeclaration) {
???
}
解决方案
我发现检查类型标志TypeFlags.Enum
是不可靠的(可能是编译器 api 中的一个错误,但我有点懒得去研究它)。我所做的是获取ts.Type
' 符号并检查其值声明是否为枚举声明。
这是未经测试的,但应该给你基本的想法:
function isEnumType(type: ts.Type) {
// if for some reason this returns true...
if (hasFlag(type.flags, ts.TypeFlags.Enum))
return true;
// it's not an enum type if it's an enum literal type
if (hasFlag(type.flags, ts.TypeFlags.EnumLiteral) && !type.isUnion())
return false;
// get the symbol and check if its value declaration is an enum declaration
const symbol = type.getSymbol();
if (symbol == null)
return false;
const { valueDeclaration } = symbol;
return valueDeclaration != null && valueDeclaration.kind === ts.SyntaxKind.EnumDeclaration;
}
function hasFlag(type: ts.Type, flag: ts.TypeFlags) {
return (type.flags & flag) === flag;
}
检查它是否是一个对象更容易一些......
function isObjectType(type: ts.Type) {
return hasFlag(type.flags, ts.TypeFlags.Object);
}
顺便说一句,如果您不熟悉它,可以从类型检查器中检索节点的类型:
const type = typeChecker.getTypeAtLocation(typeReference);
推荐阅读
- asp.net - 页面刷新时将检查项目数据保存到会话
- python - 我的 CP-SAT 程序(Google OR 工具)不执行 on_solution_callback 函数内的命令
- spring-boot - 另一个应用程序中的 Spring Boot 配置属性为空
- javascript - Websocket onmessage 未触发 - 基于 Java 的服务器
- r - 在变异中使用 str_subset 和 case_when?
- symfony4 - 如何将数据传递到 CollectionType 表单并在那里获取?
- python - python PyPDF2 - 在打印pdf文件中的文本时打印特殊字符?
- javascript - 遍历lis,如果文本包含,.click();
- flutter - Provider 方法在从另一个页面调用时返回 null
- azure-sql-database - 具有分片管理的 Azure SQL 能否在不同区域拥有数据库?