-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
TS regression: Narrowing of MathNodes by type #2810
Comments
I think this is a consequence of #2772. I haven't thought about it enough, but would be nice if the goals of that PR could be achieved while still allowing the narrowing., |
Ai, good point. I think we can solve this by defining functions like if (isFunctionAssignmentNode(node)) {
// ...
} instead of if (node.type === 'FunctionAssignmentNode) {
// ...
} anyone interested in working out a fix? Probably by defining typeguards? |
@josdejong Those exist as typeguards already! Apparently I wrote the typedefs for them...not sure why I started using Yes, I think that's a good solution. Feel free to close this. Might be worth adding a note in the 11.3.0 release notes about this, though. Also, #2772 was missing its PR link in the release notes. |
Right, #2772 removed the status of MathNode being a discriminated union. Aren't all the custom type guards already provided? The PR explicitly changed But note that providing a series of type guards isn't quite the same as a discriminated union. At least, you now can't handle a Node with a switch statement on its .type property and have it be narrowed in each case, and you can't do exhaustiveness checking on code that handles nodes (TypeScript no longer has any idea what "all the node types" are.) So the question becomes which is more important: type narrowing just by checking .type, switch statements, and exhaustiveness checking (pre 11.3) or client extensibility of the node hierarchy while having to use custom type guards (post 11.3)? Unless there's some way to extend a discriminated union, which I doubt. @ChristopherChudzicki do the custom type guards cover your use cases? |
Comments crossed. Sounds like the custom type guards are good enough. |
😂 that is good news! I've updated the history to note this, good point |
(And in some sense it means that 11.3 was inadvertently a breaking change, despite not being a major revision number.) |
yes indeed... not an intended one. Sometimes a new feature is a bug at the same time 😅 |
Hmmm, it turns out it would be possible to get both the discriminated union and client extensibility, see the answer to https://stackoverflow.com/questions/46392758/widen-tagged-discriminated-union-in-typescript-in-a-different-module, at the cost that clients that wanted to do so would need to use the |
Thanks for figuring this out, this is interesting to know. What are you're opinions @ChristopherChudzicki @mattvague? I don't have a strong opinion on this. |
Interesting. Well, if we were forced to choose supporting either
If we revert 2772 we still have to do a little bit of work to make the |
(I'm also happy to attack that since I was the culprit here 😆) |
Oh yes, that's what I meant by "revert/replace", sorry I was speaking in too abbreviated a fashion. And yes, per that StackExchange answer, there is some extra stuff that needs doing to make both things work: you have to create an interface that has all of the node types, because interfaces are extensible in this manner but apparently unions cannot be directly extended with this |
I agree with @mattvague that
Aside: I don't want to sidetrack us here, but is there a discussion anywhere about use-cases for custom node types? I am curious. The SO post that @gwhitney found seems good: not too complicated to implement, and not too big a burden on people in userland extending the library. And there is value in the discriminated union for consumers as opposed to typeguards...e.g., I know it's a small thing, but I really appreciate the intellisense / code completion offered by the discriminated union: So yeah, IMO, worth doing! |
@mattvague gives a brief description of a use case in his intro to #2775, so you could look there, and he might have more to say on this topic. |
Thanks for the feedbacks guys. Yes, it would be nice to be able to support both (in which case using So @mattvague I understand you would like to give it a try? |
To be clear, only extending the collection of node types would require this syntax... |
Yes indeed 👍 . I'll update my comment to prevent any future confusion |
Yep! Going to spend a bit of time today on this. |
Got a start here, but still some stuff to figure out: #2820 |
I took a quick look at the first typescript error, and it seems to me that transform1 is declared to return an |
@mattvague I just took a look at your branch and made a PR (goodproblems#2) that updates it to not have errors.I did this a bit quick and have to run now, so forgive my brevity! |
Status update: responded to @ChristopherChudzicki's PR on my PR. |
Describe the bug
Prior to v11.3 (not sure how far back, but certainly at least 10),
MathNode
would be narrowed based on itstype
property:To Reproduce
The text was updated successfully, but these errors were encountered: