Skip to content
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

Getting the generic type of the wrapping function #900

Closed
yavulan opened this issue Dec 10, 2020 · 2 comments
Closed

Getting the generic type of the wrapping function #900

yavulan opened this issue Dec 10, 2020 · 2 comments
Labels

Comments

@yavulan
Copy link

yavulan commented Dec 10, 2020

Thank you for this awesome project!

I want foo function to generate ts code depending on the generic type passed when calling this function.
Is it possible for ts-morph to get the generic type of the wrapping function?

function foo<T>() {
  // ts-morph methods to get the current generic type, e.g. "string" in case called `foo<string>()`
  // ...
}

In case it is not possible, is there other way to pass the type to the ts-morph inside the body of the function? Maybe through the type of an argument or something else?

Thank you!

@dsherret
Copy link
Owner

dsherret commented Dec 11, 2020

Hey @yavulan, if I'm understanding correctly, I believe probably the easiest way to solve this is to just change all the the call sites like foo<number>() to have the necessary code. I think you could just replace the expressions with an immediately invoked function containing the specify logic (ex. replace foo<string>() with (() => { /* your code goes here that deals with string */ })()). The node.replaceWith(writer => {}) function will be useful here.

By the way, here's an example that uses the compiler api directly and babel to change the call sties: https://github.com/dsherret/ts-nameof (doesn't use ts-morph)

@yavulan
Copy link
Author

yavulan commented Dec 11, 2020

Thank you. ts-nameof example is very close to what I was looking for. It's a pity microsoft/TypeScript#14419 is still not implemented.

Btw, I went through some issues on the issue tracker for ts-morph. And it's incomparable to most issue trackers for popular libraries - this issue tracker is very welcoming and useful. THANK YOU!

@yavulan yavulan closed this as completed Dec 11, 2020
@dsherret dsherret reopened this Apr 3, 2021
@dsherret dsherret closed this as completed Apr 3, 2021
Repository owner locked and limited conversation to collaborators Apr 3, 2021
Repository owner unlocked this conversation Apr 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants