-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Parse generic type #810
Comments
@GrandSchtroumpf I get the following output instead: type.getTypeArguments().map(a => a.getText()); // [] What's going on here is the TS compiler is "interning" the type to In this scenario, instead of getting the type you can get the type node. Here's an example: const project = new Project({ useInMemoryFileSystem: true });
const file = project.createSourceFile("file.ts", `type Component<type, selector> = type;
definition class MyComponent {
static selector: Component<string, '[button]'>;
}`);
// Using https://ts-ast-viewer.com can be useful to figure out what these nodes are...
// You can also use the functions like `Node.isTypeReferenceNode(node)` to check
// if a node is of a certain kind.
const propertyDec = file.getClassOrThrow('MyComponent')
.getStaticPropertyOrThrow('selector') as PropertyDeclaration;
const typeNode = propertyDec.getTypeNodeOrThrow() as TypeReferenceNode;
console.log(typeNode.getTypeArguments().map(a => a.getText())); // ['string', "'[button]'"] |
Amazing, thanks a lot !! So many kind of nodes ^^. |
Hello,
I would like to extract the value "[button]" of the generic type below for example:
I did :
It looks like
getTypeArguments
returns the value returned by the type and not the text.Unfortunately I cannot use
type.getText()
and string manipulation because the realworld usecase is too complex.I was looking for something like
type.isGeneric() ? type.getGeneric() : []
but did find anything that looks like that.Thanks for the help :)
The text was updated successfully, but these errors were encountered: