Automatic "full" discriminated union
Hello, I have this code:
But in this case, I have to add
field?: 'never'
to make the compiler happy when destructuring the validated input, else it says that some field doesn't exist on it.
Is there a way to make this work, or is my approach wrong from the start? Thank you! :D13 Replies
First of all, there is
'+': 'reject'
aka .onUnknownKey('reject')
Tried both to add the + field to each definition, or apply the onUnknownKey to the final Input, and both dont fix the issue
Nah that's another stuff, just telling about it
That's useful, just not for solving this specific issue :D
David, wait you don't have
xor
type?
@JameEnderThat seems like a working solution, yet i wanted to reduce the complexity, not the other way around :D
It seems weird that the library can't do it on its own
Yes, I've told David we gonna need this stuff in #Dynamic type based on another value
So you are the perfect example of someone who actually need this to prove my point
Neat
Remind me to ping hit tomorrow ( I try not to ping him on weekends idk why )
What you're trying to do here with the destructing is quite clearly wrong from a TS perspective, so the error is correct
You'd need to narrow on
runMode
before you can access exactData
/datasetId
/...
That's the correct way to use discriminated unions @JameEnder
You don't need any of the complexity that @Dimava the Monoreaper gave for this@JameEnder your opinion?
🤔 does TS narrows on checks on deconstructed stuff?
I'll test it out later today and let you know
Unfortunately not. So you have to do the check on
input.runMode
and not a deconstructed runMode
Or at least when I tried it a few months ago that was the case