typescript compiler api - How to detect if property type is enum or objectHow do you explicitly set a new property on `window` in TypeScript?Type definition in object literal in TypeScriptTypeScript Objects as Dictionary types as in C#Are strongly-typed functions as parameters possible in TypeScript?How to convert string to enum in TypeScript?Typescript `enum` from JSON stringTypescript: Interfaces vs TypesHow to Set the properties of typescript object in angular2?Typescript object property typingsHow to initialize an object in TypeScript
What are the differences between the usage of 'it' and 'they'?
can i play a electric guitar through a bass amp?
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
What defenses are there against being summoned by the Gate spell?
Why, historically, did Gödel think CH was false?
The use of multiple foreign keys on same column in SQL Server
Why doesn't H₄O²⁺ exist?
Test if tikzmark exists on same page
Which models of the Boeing 737 are still in production?
Approximately how much travel time was saved by the opening of the Suez Canal in 1869?
Minkowski space
Risk of getting Chronic Wasting Disease (CWD) in the United States?
Today is the Center
What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)
Is this a crack on the carbon frame?
What's the point of deactivating Num Lock on login screens?
Why are 150k or 200k jobs considered good when there are 300k+ births a month?
What does it mean to describe someone as a butt steak?
Why do I get two different answers for this counting problem?
Voyeurism but not really
In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?
Mage Armor with Defense fighting style (for Adventurers League bladeslinger)
Prove that NP is closed under karp reduction?
Can I make popcorn with any corn?
typescript compiler api - How to detect if property type is enum or object
How do you explicitly set a new property on `window` in TypeScript?Type definition in object literal in TypeScriptTypeScript Objects as Dictionary types as in C#Are strongly-typed functions as parameters possible in TypeScript?How to convert string to enum in TypeScript?Typescript `enum` from JSON stringTypescript: Interfaces vs TypesHow to Set the properties of typescript object in angular2?Typescript object property typingsHow to initialize an object in TypeScript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have to know each property declaration node, if is enum or another object.
so i get the type reference :
const typeReferance = (arrayType as any).typeName
But i don't have any idea how to detect if is enum, or object.
Models:
enum Color Red, Green, Blue
class SomeObject
string: string;
class Model
color:Color;
The visitor:
if (node.kind === ts.SyntaxKind.PropertyDeclaration)
???
typescript typescript-compiler-api
add a comment |
I have to know each property declaration node, if is enum or another object.
so i get the type reference :
const typeReferance = (arrayType as any).typeName
But i don't have any idea how to detect if is enum, or object.
Models:
enum Color Red, Green, Blue
class SomeObject
string: string;
class Model
color:Color;
The visitor:
if (node.kind === ts.SyntaxKind.PropertyDeclaration)
???
typescript typescript-compiler-api
add a comment |
I have to know each property declaration node, if is enum or another object.
so i get the type reference :
const typeReferance = (arrayType as any).typeName
But i don't have any idea how to detect if is enum, or object.
Models:
enum Color Red, Green, Blue
class SomeObject
string: string;
class Model
color:Color;
The visitor:
if (node.kind === ts.SyntaxKind.PropertyDeclaration)
???
typescript typescript-compiler-api
I have to know each property declaration node, if is enum or another object.
so i get the type reference :
const typeReferance = (arrayType as any).typeName
But i don't have any idea how to detect if is enum, or object.
Models:
enum Color Red, Green, Blue
class SomeObject
string: string;
class Model
color:Color;
The visitor:
if (node.kind === ts.SyntaxKind.PropertyDeclaration)
???
typescript typescript-compiler-api
typescript typescript-compiler-api
edited Mar 28 at 21:10
David Sherret
54.1k17127129
54.1k17127129
asked Mar 8 at 3:58
yantrabyantrab
342319
342319
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I've found checking the type flag for TypeFlags.Enum is not reliable (maybe a bug in the compiler api, but I've been kind of lazy to look into it). What I do is get the ts.Type's symbol and check if its value declaration is an enum declaration.
This is untested, but should give you the basic idea:
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;
Checking if it's an object is a little easier...
function isObjectType(type: ts.Type)
return hasFlag(type.flags, ts.TypeFlags.Object);
By the way, in case you are not familiar with it, the type of a node can be retrieved from the type checker:
const type = typeChecker.getTypeAtLocation(typeReference);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056515%2ftypescript-compiler-api-how-to-detect-if-property-type-is-enum-or-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've found checking the type flag for TypeFlags.Enum is not reliable (maybe a bug in the compiler api, but I've been kind of lazy to look into it). What I do is get the ts.Type's symbol and check if its value declaration is an enum declaration.
This is untested, but should give you the basic idea:
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;
Checking if it's an object is a little easier...
function isObjectType(type: ts.Type)
return hasFlag(type.flags, ts.TypeFlags.Object);
By the way, in case you are not familiar with it, the type of a node can be retrieved from the type checker:
const type = typeChecker.getTypeAtLocation(typeReference);
add a comment |
I've found checking the type flag for TypeFlags.Enum is not reliable (maybe a bug in the compiler api, but I've been kind of lazy to look into it). What I do is get the ts.Type's symbol and check if its value declaration is an enum declaration.
This is untested, but should give you the basic idea:
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;
Checking if it's an object is a little easier...
function isObjectType(type: ts.Type)
return hasFlag(type.flags, ts.TypeFlags.Object);
By the way, in case you are not familiar with it, the type of a node can be retrieved from the type checker:
const type = typeChecker.getTypeAtLocation(typeReference);
add a comment |
I've found checking the type flag for TypeFlags.Enum is not reliable (maybe a bug in the compiler api, but I've been kind of lazy to look into it). What I do is get the ts.Type's symbol and check if its value declaration is an enum declaration.
This is untested, but should give you the basic idea:
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;
Checking if it's an object is a little easier...
function isObjectType(type: ts.Type)
return hasFlag(type.flags, ts.TypeFlags.Object);
By the way, in case you are not familiar with it, the type of a node can be retrieved from the type checker:
const type = typeChecker.getTypeAtLocation(typeReference);
I've found checking the type flag for TypeFlags.Enum is not reliable (maybe a bug in the compiler api, but I've been kind of lazy to look into it). What I do is get the ts.Type's symbol and check if its value declaration is an enum declaration.
This is untested, but should give you the basic idea:
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;
Checking if it's an object is a little easier...
function isObjectType(type: ts.Type)
return hasFlag(type.flags, ts.TypeFlags.Object);
By the way, in case you are not familiar with it, the type of a node can be retrieved from the type checker:
const type = typeChecker.getTypeAtLocation(typeReference);
answered Mar 28 at 21:10
David SherretDavid Sherret
54.1k17127129
54.1k17127129
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056515%2ftypescript-compiler-api-how-to-detect-if-property-type-is-enum-or-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown