-
Notifications
You must be signed in to change notification settings - Fork 328
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
Decoder.sum is not typesafe, What is recommended to use between Decoder.union and Decoder.sum? #523
Labels
experimental
something related to the experimental features
Comments
Came here to report this issue with |
Yes
Alas that's right, note that the proposed change would make non- import * as D from 'io-ts/lib/Decoder'
const sum = <T extends string>(tag: T) => <A>(
members: { [K in keyof A]: D.Decoder<unknown, A[K] extends { [Key in T]: K } ? A[K] : never> }
): D.Decoder<unknown, A[keyof A]> => D.sum(tag)(members)
const CircleD = D.type({
kind: D.literal(1),
radius: D.number
})
const SquareD = D.type({
kind: D.literal(2),
x: D.number
})
const ShapeD = sum('kind')({
1: CircleD, // error
2: SquareD // error
}) |
@gcanti following does work though (not sure how, maybe typescript bug?) const ShapeD = sum('kind')({
[1]: CircleD,
[2]: SquareD
}) |
gcanti
added a commit
that referenced
this issue
Oct 21, 2020
@kpritam nice trick! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have two doubts:
Difference between
Decoder.union
andDecoder.sum
Decoder.sum
optimized and should be used whenever possible?Decoder.sum
is not typesafesum
,key
ofobject
can be different than the correspondingvalue
Above issue can be resolved if signature of
sum
is changed to following:Happy to submit PR if this make sense.
The text was updated successfully, but these errors were encountered: