Declared Interface Properties vs Apparent Interface Properties in Typescript

3 weeks ago 17
ARTICLE AD BOX

I am currently working on an automation for a typescript project. An important step for that automation consists in the automated extraction of all property names of an interface X, including the properties of interfaces from which the interface X inherits from. This should include cases like:

interface X extends Y,Z

but also UtilityType cases like:

interface X extends Omit<V,'nameOfPropToIgnore' | 'nameOfOtherPropToAlsoIgnore'>

My tool of choice for any non-runtime operations related to Typescript is ts-morph. In this context, I first thought that doing as described above requires me to:

scan a typescript file for interface definitions

for each detected interface, get all of its properties > names

for each detected interface, check if they have extends clauses, and for each one, repeat steps 1 - 3 (recursively).

Then, after some back-and-forth with my AI assistant, I got to know that doing simply this does it all, without the need of recursion:

// 1. Get the Type object representing this interface const type = interf.getType(); // 2. Use getApparentProperties() to get all symbols // (this includes inherited ones and handles Omit/Pick/etc.) const properties = type.getApparentProperties(); // 3. Extract the names from the symbols const allPropertyNames = properties.map(symbol => symbol.getName());

I tested this and it works perfectly. But here's the thing, ts-morph explicitly mentions this difference about getting the properties of a type vs getting the apparent properties.

Yet I cannot find anything about that difference regarding the typescript compiler API online. I would like to more precisely understand this concept of declared vs apparent properties of an interface type before blindly applying the code my AI assistant recommended above. So what is it all about regarding this distinction between declared vs apparent properties of an interface in the typescript compiler API ? Is it really the right way to go to solve my problem ?

Read Entire Article