Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] What's the best way to get the constructors of all the parameters of a method?

See original GitHub issue

Hey there!

First of all, congratulations on this AMAZING AMAZING AMAZING project! I’m astonished by the flexibility it provides and how much easier it is than using the typescript compiler API.

I’m a beginner to the typescript compiler API, so I still have some questions:

Say I have this example code

type Person = {
  firstname: string;
  age: 34;

class Place {
  private lat;
  private lng;
  constructor(lat: 123, lng: 543) { = lat;
    this.lng = lng;

export class Animal {
  bark(toWhom: Person, howManyTimes: 4) {
    return "bark!";

I want to create a program that given a class name (say Animal), will pick a random method of it (say bark) and call it with parameters that make sense.

For that, I need to be able to

  1. Given a class name, get the class
  2. Given a class, get all the methods
  3. Given a specific method get all the parameters
  4. Given a list of parameters, get their respective types
  5. Given a list of types, get how to construct an instance of each type.

I’ve managed to solve 1-4. However 5) is eluding me.

Here’s what I have so far

import { Project } from "ts-morph";

const project = new Project({
  tsConfigFilePath: "./tsconfig.json",

const sourceFiles = project.getSourceFile((f) => f.getClasses().length === 2);
const aClass = sourceFiles?.getClasses()[1]; // Animal
const methods = aClass?.getMethods();
const aMethod = methods![0]; // Animal.bark
const theParams = aMethod.getParameters();
const aParam = theParams[0]; // toWhom

const paramType = aParam.getTypeNode();
console.log(paramType?.getKindName()); // TypeReference
console.log(paramType?.getText()); // Person

Now I need to get “ahold” of the Person class in order to be able to read its constructor and the types of the constructor params to know how to create an instance of it.

But how would I go about doing that?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

dsherretcommented, Mar 1, 2021

@mikealche oh, my bad! I mixed this up in my head and was acting like this was typeof Person.

In this case, you can get the parameter’s type, then from the type go to the symbol and then to the class declaration:

const classDecl = toWhomParam.getType().getSymbol()?.getDeclarations()[0];
if (Node.isClassDeclaration(classDecl)) {
  for (const ctor of classDecl.getConstructors()) {
dsherretcommented, Mar 1, 2021

@mikealche no problem! 🙂 Feel free to open another issue if you have more questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing Information to a Method or a Constructor
You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't...
Read more >
(Java) Pass a parameter from a constructor to all the methods ...
First two important things : Constructors are designed to create instances. Class names should start with an uppercase. As you write :
Read more >
Constructors in Java - GeeksforGeeks
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values,...
Read more >
How do you decide if a parameter should go to the constructor ...
Whenever I create a class I am never sure whether I should put a parameter/attribute in the constructor or in the method the...
Read more >
5.2. Writing Constructors — AP CSAwesome
Fill in the code for the 2 constructors that are numbered 1 and 2. And fill in the code to call the constructors...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found