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
OperatorNode
type should accept generics for 'op' and 'fn'
#2575
Comments
Thanks. It makes sense to refine the In your example demonstrating the issue I think TypeScript has a point, and you probably should not use Thinking aloud here, would the following work? interface AdditionNode extends OperatorNode {
op: '+'
fn: 'add'
}
export function isAdditionNode(node: MathNode): node is AdditionNode {
return isOperatorNode(node) && node.fn === 'add'
} Thanks for implementing a generics based solution in #2576, that looks neat! I think it's a better solution than my idea here ☝️ |
Hmm, why do you think it's a bad plan to have
I thought about that, but it would mean a fair bit of extra work to define all the subtypes for exist operators, plus adding an extra burden for new ones which is why I like the generics approach
Oh ok cool, this sounds like you're agreeing with my approach 😄 |
It's not "wrong" perse, but since both
Yes. Sorry if I confused you by thinking aloud too much 😂 |
The Problem
Right now all operator types share the same basic
OperatorNode
type. This has a few consequences:new OperatorNode('*', 'mult')
For example, say I've defined my own custom type guards like so
If I try to then use them like this
Typescript is sad and upset because it thinks that the first type guard should have caught any operator node and therefore you could never have the second case (explaining the
never
type)The Solution
If we added generic args to OperatorNode for
op
andfn
typescript would warn us when making invalid operator nodesand would allow doing this with type guards:
Which then makes typescript very happy 🌈
The text was updated successfully, but these errors were encountered: