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

Replace Node interface with discriminated unions #56275

Open
5 tasks done
JoshuaKGoldberg opened this issue Oct 31, 2023 · 4 comments
Open
5 tasks done

Replace Node interface with discriminated unions #56275

JoshuaKGoldberg opened this issue Oct 31, 2023 · 4 comments
Labels
Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Oct 31, 2023

πŸ” Search Terms

discrimnated type union node internal source api

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

Happy Halloween πŸŽƒ! In the spirit of the holiday, I'd like to resurrect #13634: using a discriminated unions for Node categories. Roughly paraphrasing #18285's very good description here:

As mentioned in #18214 (comment), TypeScript's source can remove a lot of casts by using a discriminated union for Node.
While this should not affect TypeScript's runtime performance (as it just changes its types, mostly) it may cause TypeScript to take longer to compile itself.

#13634 was declined in 2018 for performance concerns (#13634 (comment)) but I hope that the years of performance improvements since then have since made this possible.

πŸ“ƒ Motivating Example

Switching TypeScript's Node from an interface from a discriminated type union would allow .kind checks to narrow types of values declared as Node.

import * as ts from "typescript";

declare const node: ts.Node;

if (node.kind === ts.SyntaxKind.FunctionDeclaration) {
  node.name?.text;
  //   ~~~~
  // Property 'name' does not exist on type 'Node'.
}

πŸ’» Use Cases

In TypeScript itself, a match-case-and-full-word search for /(?:name|node|parent) as \w{5,}/ in src/**/*.ts excluding *test* yields 1879 matches right now. After prototyping it locally and enabling @typescript-eslint/no-unnecessary-type-assertion, I mostly-automatically reduced it to 987 matches. More removals could likely be enacted with manual effort.

In community projects that use the TypeScript AST, including typescript-eslint, we'd similarly be able to have more precise type information on TS AST nodes.

#13634 mentions other consumers too, such as Babel's https://github.com/babel/babel/blob/c446ff85c28e117ebf3cd72cd34ef358f1077aa8/packages/babel-types/src/validators/is.ts.

@jakebailey
Copy link
Member

@weswigham I know you were working on this somewhat recently too

@Andarist
Copy link
Contributor

For reference: Wes' PR can be found here

@weswigham
Copy link
Member

Yeah, I have a recentish branch there, the 2.7x slowdown in compilation time and editor performance hurts though. Personally, I'm of the opinion that we should just eat the difference and use the worsened conditions as a forcing function for self-improvement, but that order of operations isn't a popular one :P

So... we probably can't take the change until we get the perf closer, which means looking into those traces @jakebailey took and trying to optimize those cases.

@jakebailey
Copy link
Member

It really goes to show how bad my memory is to not remember that there already was a PR, and also I reviewed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants