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

Add support for TypeScript class constructor in TypeScript mode #131

Open
briandipalma opened this issue Mar 9, 2022 · 15 comments · Fixed by #138
Open

Add support for TypeScript class constructor in TypeScript mode #131

briandipalma opened this issue Mar 9, 2022 · 15 comments · Fixed by #138

Comments

@briandipalma
Copy link

Describe the issue

From the TypeScript documentation: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html
We can use class constructors in params:

/**
 * A parameter can be a class constructor, using Google Closure syntax.
 *
 * @param {{new(...args: any[]): object}} C - The class to register
 */
function registerClass(C) {}

But this fails to parse when using the ESLint JSDoc plugin.

@simonseyock
Copy link
Member

Thanks for the report! I will look into it.

@brettz9
Copy link
Collaborator

brettz9 commented May 6, 2022

The construct signature with new keyword can apparently be used with arrow style too:

function greet(ctor: new () => Base) {
  const instance = new ctor();
  instance.printName();
}

https://www.typescriptlang.org/docs/handbook/2/classes.html#abstract-construct-signatures

FWIW, this is a high priority for us for a PR with the purpose of generating a declaration file for espree after some jsdoc manipulations (hopefully eventually leading to the same for ESLint).

@brettz9
Copy link
Collaborator

brettz9 commented May 9, 2022

@simonseyock : any chance of being able to take a look at this in the near future?

@simonseyock
Copy link
Member

@brettz9 I will look into this in the next days.

@jsdoc-type-pratt-parser-chores
Copy link
Contributor

🎉 This issue has been resolved in version 3.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@brettz9
Copy link
Collaborator

brettz9 commented Jun 10, 2022

I'm not seeing this item from the OP work in the tester: {new(...args: any[]): object}

@brettz9
Copy link
Collaborator

brettz9 commented Jun 15, 2022

Am reopening as the specific case of an object with the new keyword indicating a constructor isn't yet resolved.

@brettz9 brettz9 reopened this Jun 15, 2022
@brettz9
Copy link
Collaborator

brettz9 commented Jun 19, 2022

@simonseyock : Any chance you might have some time to take a look?

@simonseyock
Copy link
Member

the type expression would be new(...args: any[]): object not {new(...args: any[]): object}. The former does work in the tester, the latter does not make sense for me.

@simonseyock
Copy link
Member

ok I might not have understood that correctly. I will see what I can do.

@simonseyock
Copy link
Member

This might be complicated as this has to do with the whole problematic of needing a different parsing context inside object literals. This is in work, but will need some time.

@brettz9
Copy link
Collaborator

brettz9 commented Jul 4, 2022

Much appreciated, thanks!

This feature should be particularly handy since without defining and pointing to an actual class, it is the only way to indicate the pattern of a constructor (and define the class' static properties between the same curly brackets). In cases like the PR I'm working with, one may have a factory which produces a class rather than having any publicly exported class that can be pointed to.

@brettz9
Copy link
Collaborator

brettz9 commented Nov 7, 2022

@simonseyock : I know you said it'd need some time, but just wondering if you had any status update to share? Thanks!

@simonseyock
Copy link
Member

simonseyock commented Nov 14, 2022

I looked into this again today, but I did not find a solution that worked properly. The problem with the contexts is solvable, but no I hit some problems with the types of the results. The problem is that I use one result type for jsdoc and typescript syntax but it is at some points so different that it does not really make sense anymore. I am thinking about splitting the result types and to make the Parser generic. But I don't know if that works properly.
Sorry for being so inconsistent, but theses changes need a lot of work and it is hard for me to find that much time in short succession.

@brettz9
Copy link
Collaborator

brettz9 commented Nov 14, 2022

No worries! Appreciate all the investigations and effort...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants