-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
taggedUnion tag / discriminator isn't typed #1153
Comments
Hi @dearlordylord – I just took a look at your snippet, but both seem to work as expected. Maybe I'm missing your point? The first snippet declares you have an object literal that is expected to have a field
In your second snippet, you flip the order of "type" field literals. Unfortunately, this is never going to work, as the first layer of checks will decide which decoder to use (in your case, it will look up which decoder to use for the "listener" field), and use that decoder on the input again. But since that decoder has defined This may seem a bit un-DRY at first, but it's a necessary duplication because of TypeScript and runtime limits. If you care about the DRY-ness, you could use Let me know if this answers your question. |
Thank you for taking time to explain this. I want to add that type-level DRY doesn't concern me as much as runtime-level DRY; if I have to repeat myself, but I'm also checked by the compiler, it's completely fine. Here I'm not checked by the compiler though as I can write whatever into the discriminator field (I'll call it discriminator because it's more common name, e.g. https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions references it ) I'm also coming from https://github.com/effect-ts/effect/tree/main/packages/schema#discriminated-unions https://github.com/colinhacks/zod?tab=readme-ov-file#discriminated-unions https://gist.github.com/ssalbdivad/d60d876ab6486adc97e38e3f6916e93f who seem to overcome typescript limitations and make it "not compile" on wrong inputs provided So, to reiterate, I'm fine with
but I'm not fine with
or with
|
Description of the bug
taggedUnion tag / discriminator is typed as a "string" not checking against field existence / field (constant) content
both parses would fail
Expected behavior
this shouldn't compile
To Reproduce
Additional context
Decoders version: 2.4.2
TypeScript version: 5.4.5
Flow version: ?
Node version: browser
The text was updated successfully, but these errors were encountered: