How to analyze Type object from TypeScript compiler API?

Viewed 147

Note: This question is about the TypeScript compiler API, not about TypeScript as a language!


I have a value of type ts.Type. I want to know what type it represents: Is it a number? A function? An array?

I found that type.flags allows me to perform roughly the static equivalent of the typeof operator, allowing me to differentiate between primitive types such as number, string, and boolean (but not functions). Anything else -- arrays, functions, POJOs, etc. -- simply has the TypeFlags.Object flag.

How can I further examine these non-primitive types? How do I determine whether a type is an array, a function, an enum, a class instance...?

1 Answers

There is another field on types that have Typeflags.Object. Its called type.objectFlags and you can use it to further distinguish the type

Related