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

Make a "shortcut" API around TypeScript for node types #6244

Closed
JoshuaKGoldberg opened this issue Dec 18, 2022 Discussed in #6013 · 1 comment
Closed

Make a "shortcut" API around TypeScript for node types #6244

JoshuaKGoldberg opened this issue Dec 18, 2022 Discussed in #6013 · 1 comment
Assignees
Labels
accepting prs Go ahead, send a pull request that resolves this issue repo maintenance things to do with maintenance of the repo, and not with code/docs
Milestone

Comments

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Dec 18, 2022

Discussed in #6013

Originally posted by JoshuaKGoldberg November 7, 2022

Suggestion

We write code like this very frequently:

const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(node);

It's a little burdensome.

@bradzacher and I were chatting in a pairing about how it might be convenient to make a wrapper around this, for convenience. Vague first draft:

declare const typeUtils: {
  [K in keyof ts.TypeChecker]:
    (services: ParserServices, node: TSESTree.Node) => ReturnType<ts.TypeChecker[K]>;
};

const type = typeUtils.getTypeAtLocation(parserServices, node);

We'd probably want to export this as a publicly available & documented API, so it's not confusing to consumers why our source code doesn't look like examples.

One downside would be that we're obfuscating how to use TypeScript APIs. I hate to add to complexity of understanding these already hard-to-understand things...


We ended up with #6013 (comment): putting the types right on the parser services.

I would say we could probably go one step further and just offer all of this off of the parser serivces!

For example:

type ParserServices = {
  getTypeAtLocation(node: TSESTree.Node): ReturnType<ts.TypeChecker['getTypeAtLocation']>;
};

const parserServices = util.getParserServices(context);

parserServices.getTypeAtLocation(node);

That would make it even more straight-forward for people to use the tooling!

@JoshuaKGoldberg JoshuaKGoldberg added repo maintenance things to do with maintenance of the repo, and not with code/docs accepting prs Go ahead, send a pull request that resolves this issue labels Dec 18, 2022
@JoshuaKGoldberg JoshuaKGoldberg self-assigned this Dec 18, 2022
@JoshuaKGoldberg JoshuaKGoldberg changed the title Discussion: Consider making a "shortcut" API around TypeScript for node types Make a "shortcut" API around TypeScript for node types Dec 18, 2022
@JoshuaKGoldberg JoshuaKGoldberg added this to the 6.0.0 milestone Jan 31, 2023
@JoshuaKGoldberg
Copy link
Member Author

Merged in #6404. This'll be part of the v6 release.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue repo maintenance things to do with maintenance of the repo, and not with code/docs
Projects
None yet
Development

No branches or pull requests

1 participant