7

I want to merge class declarations in a .dt.s file to generate a cleaner public API. I am stuck on how to make this work with generic type arguments. Let's say I have:

class A1<T> { // Non-exported class I want to hide
  data?: T;
}

export class B1 extends A1<string> {
}

Ideally, I want to turn this into:

export class B1 {
  data?: string;
}

I can get the type of A1 and then copy its members. But how do get a resolved version of A1 that uses string instead of T?

For reference, this is my current code:

for (const heritageClause of node.heritageClauses) {
  for (const type of heritageClause.types) {
    if (isExported(type.modifiers)) {
      exportedTypes.push(type);
    } else {
      const privateType = typeChecker.getTypeAtLocation(type);
      if (privateType?.symbol?.members) {
        privateType.symbol.members.forEach((definition, memberName) => {
          if (!currentMembers || !currentMembers.has(memberName)) {
            additionalMembers.push(...definition.declarations);
           }
         }
      });
    }
  }
}

1 Answer 1

4

I believe the method you are looking for is TypeChecker#getTypeOfSymbolAtLocation(symbol, node).

The following should get the resolved type of string | undefined:

// by the way, recommend renaming `type` to `typeNode` to avoid confusion
typeChecker.getTypeOfSymbolAtLocation(privateType.getProperties()[0], type);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.